As commented in the code , I need a wait before checking the else if
after writeFile(lFileData);
. How to achieve this?
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()
}
if(i <= 1){
writeFile(lFileData);
doAdelay();
};
and:
function doAdelay(){
setTimeout(function(){return true;},30000);
};
Hope this helps
您可以在elseif函数中使用set interval
setTimeout(function,3000);
So that means the writeFile function is asynchronous?
I would create a callback function in the writeFile function itself and then do the 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.
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 "
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() :
function performFunctionXAfterDelay() {
// 1000 ms delay
window.setTimeout(functionX,1000)
}
function functionX() {
// YOUR TIME DELAYED CODE
}
var t = setInterval("javascript expression", milliseconds);
clearInterval(t);
u can use setInterval
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. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.