在下一个动作之前等待几秒钟 - Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13284583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wait for few seconds before next action - Javascript
提问by AnhSirk Dasarp
As commented in the code , I need a wait before checking the else if
after writeFile(lFileData);
. How to achieve this?
正如代码中所评论的,在检查else if
after之前我需要等待writeFile(lFileData);
。如何实现这一目标?
for(var i=0;i<mLocalStorageCount;i++)
{
if(i <= 1)
{
writeFile(lFileData); //This action takes a lot of time with its call backs. I need a wait here.
}
else if(i > 1 && i <=3)
{
someOtherfun()
}
回答by AnhSirk Dasarp
if(i <= 1){
writeFile(lFileData);
doAdelay();
};
and:
和:
function doAdelay(){
setTimeout(function(){return true;},30000);
};
Hope this helps
希望这可以帮助
回答by Akhilraj N S
You can use set interval in elseif function
您可以在 elseif 函数中使用设置间隔
setTimeout(function,3000);
回答by Martin
So that means the writeFile function is asynchronous?
那么这意味着 writeFile 函数是异步的吗?
I would create a callback function in the writeFile function itself and then do the someOtherfun().
我会在 writeFile 函数本身中创建一个回调函数,然后执行 someOtherfun()。
Edit: Due to you can not really do the rest of the iteration in the callback function (which you just said) you can do something like this:
编辑:由于您无法真正在回调函数(您刚才说的)中完成其余的迭代,您可以执行以下操作:
function writeFile () {
... here goes your function ...
if ( finished ) {
window.finished = true;
}
}
for (yourForCondition) {
if () {
window.finished = false;
writeFile();
while (!window.finished) {}
}
if () {
someOtherFun();
}
}
It's a bit dirty, but it should work. It would loop until writeFile() says he is done.
它有点脏,但它应该可以工作。它会循环直到 writeFile() 说他完成了。
Edit2: Will probably not work since "while (!window.finished) {} is a busy-wait loop that will peg one core to 100% and probably make the browser ask the user if the script should be killed. – Frédéric Hamidi "
Edit2:可能不会工作,因为“while (!window.finished) {} 是一个忙等待循环,它会将一个内核固定到 100% 并可能让浏览器询问用户是否应该终止脚本。 – Frédéric Hamidi”
回答by R Ghosh
You may want to rewrite the code to have the portion you want to run on a delay in its own function. From there call that function by calling performFunctionXAfterDelay() :
您可能希望重写代码,让您希望在其自己的函数中延迟运行的部分。从那里通过调用 performFunctionXAfterDelay() 调用该函数:
function performFunctionXAfterDelay() {
// 1000 ms delay
window.setTimeout(functionX,1000)
}
function functionX() {
// YOUR TIME DELAYED CODE
}
回答by Ghostman
var t = setInterval("javascript expression", milliseconds);
clearInterval(t);
u can use setInterval
你可以使用 setInterval
回答by Ayyappan Sekar
hi i think there is no need of wait before execution of "else if(i > 1 && i <=3)" code. because if "if(i <= 1)" condition is true and " writeFile(lFileData); " gets executed, control will no be given for "else" part and " someOtherfun()" will not get executed. :)
嗨,我认为在执行“else if(i > 1 && i <=3)”代码之前不需要等待。因为如果“if(i <= 1)”条件为真并且“ writeFile(lFileData); ”被执行,“else”部分将不会获得控制权,“someOtherfun()”将不会被执行。:)