依次执行函数

发布于 2024-11-07 11:18:59 字数 232 浏览 1 评论 0原文

我有这个 javascript 代码:

 if (direction !== "leftnav") { 
// DO THINGS
};
setTimeout(function () {
//DO OTHER THINGS AFTER THE FIRST THING
}, 1000);

是否可以在第一个函数之后执行 2e 函数,而不必使用超时事件?

感谢您的帮助!

I have this javascript code:

 if (direction !== "leftnav") { 
// DO THINGS
};
setTimeout(function () {
//DO OTHER THINGS AFTER THE FIRST THING
}, 1000);

Is it possible to execute the 2e function after the first instead of having to use a timeout event?

thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽你眉间 2024-11-14 11:18:59

是的,这是可能的。这是使用您提供的唯一代码的示例:

if (direction !== "leftnav") {  
    // DO THINGS...

    // THEN
    doOtherThings();

    // THEN
    doMoreThings();
}; 

var doOtherThings = function(){
    //DOING OTHER THINGS
}
var doMoreThings = function(){
    //DO EVEN MORE THINGS
}

Yes it's possible. Here's an example using the only code you've provided:

if (direction !== "leftnav") {  
    // DO THINGS...

    // THEN
    doOtherThings();

    // THEN
    doMoreThings();
}; 

var doOtherThings = function(){
    //DOING OTHER THINGS
}
var doMoreThings = function(){
    //DO EVEN MORE THINGS
}
内心激荡 2024-11-14 11:18:59

如果 if 语句中的内容被阻塞,Javascript 将从上到下运行。如果是这样,您可以将代码放在 if 语句的正下方,在超时之外,它就会正常运行。如果是异步的,您可以使用第一个函数完成时触发的回调来启动第二个函数。

下面提供的示例并不是真正有效的做事方式,而是更多地用于说明我上面所说的内容。在 write() 函数中,您可能正在执行 AJAX 调用,或者等待用户单击某些内容,或者您​​有什么。解释器继续执行下一行,该部分将立即按阻塞顺序写入。

JS

var write = function (v, cb) {
    setTimeout(function() {
        document.write(v);
        cb && cb();
    }, 1000);
}

if (true) {
    write("I'm not blocking, blah<br/>", function() {
        document.write("Running<br/>");
    });
}

if (true) {
    document.write("I'm blocking, blah<br/>");
}
document.write("Running<br/>");

输出

I'm blocking, blah
Running
I'm not blocking, blah
Running

http://jsfiddle.net/robert/ vvmyk/

Javascript will run from top to bottom if what you have in that if statement is blocking. If so, you can just put the code right below the if statement, outside of a timeout and it will run normally. If it's asynchronous, you can use a callback that fires when the first function is completed to start the second function.

The example provided below is not really efficient way of doing things, but is more used to illustrate what I'm talking about above. Within the write() function you may be doing an AJAX call, or waiting for the user to click something, or what have you. The interpreter continues on to the next line which section which will just write right away, and in blocking order.

JS

var write = function (v, cb) {
    setTimeout(function() {
        document.write(v);
        cb && cb();
    }, 1000);
}

if (true) {
    write("I'm not blocking, blah<br/>", function() {
        document.write("Running<br/>");
    });
}

if (true) {
    document.write("I'm blocking, blah<br/>");
}
document.write("Running<br/>");

Output

I'm blocking, blah
Running
I'm not blocking, blah
Running

http://jsfiddle.net/robert/vvmyk/

行至春深 2024-11-14 11:18:59

你只需要按照你想要的顺序调用函数

function foo(){
//code
}

function bar(){
//other
}

bar();
foo();

就像这样,bar将在foo之前调用。

You just need to call functions with the order you want

function foo(){
//code
}

function bar(){
//other
}

bar();
foo();

Like this, bar will be called before foo.

雪若未夕 2024-11-14 11:18:59

使用 javascript promise和 Promise.all

use javascript promise and Promise.all

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文