依次执行函数
我有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,这是可能的。这是使用您提供的唯一代码的示例:
Yes it's possible. Here's an example using the only code you've provided:
如果 if 语句中的内容被阻塞,Javascript 将从上到下运行。如果是这样,您可以将代码放在 if 语句的正下方,在超时之外,它就会正常运行。如果是异步的,您可以使用第一个函数完成时触发的回调来启动第二个函数。
下面提供的示例并不是真正有效的做事方式,而是更多地用于说明我上面所说的内容。在
write()
函数中,您可能正在执行 AJAX 调用,或者等待用户单击某些内容,或者您有什么。解释器继续执行下一行,该部分将立即按阻塞顺序写入。JS
输出
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
Output
http://jsfiddle.net/robert/vvmyk/
你只需要按照你想要的顺序调用函数
就像这样,
bar
将在foo
之前调用。You just need to call functions with the order you want
Like this,
bar
will be called beforefoo
.