开发者

Need JavaScript sleep() replacement

I know this is bad:

function sleep(millis) {
    var date = new Date();
    var curDate = null;
    do { curDate = new Date();
    } while(curDate-date < millis);
}

EDIT:
function doSomethingQuickly(pixelData) {
    // loads an external image, filling the entire screen
    // overlays $pixelsData over image
}

But I really do need this sort of functionality since doSomethingQuickly() returns so fast and the other doSomethingQuickly()'s cannot be allowed to run until the previous is finished. It would be disastrous to simply fire them all off and wait for results to deal with them.

doSomethingQuicky();
sleep(500);
doSomethingQuicky();
sleep(500);
doSomethingQuicky();
sleep(500);
doSomethingQuicky();
sleep(500);
doSomethingQuicky();
sleep(500);

My question is that since simulating sleep in JS is bad, how can I achieve the same using setTimeout() or another more 开发者_如何学Goacceptable method

NOTE: this is not in a web browser

EDIT: You can see that if it ran 5 times without the sleep, it would quickly show the final image, when what it should do is 1) show an image 2) pause for 5 seconds 3) repeatYou can see that if it ran 5 times without the sleep, it would quickly show the final images, when what it should do is 1) show an image 2) pause for 5 seconds 3) repeat


How about:

function sleep(ms,callback){
    setTimeout(callback,ms);
}
//basic usage
while (someStoppingcondition){
  sleep(500,doSomethingQuicky);
}

if doSomethingQuicky is always the same function, setInterval (see other answers) is sufficient. Make sure it will not run forever, use clear[Interval/Timeout] to stop the timers.

if your problem is that one function has to complete before the next one executes, this may be a way to solve it:

function firstRunner(arg1,arg2,/* ... argx*/, nextRunner){
   //do things
   //after things are done, run nextRunner
   nextRunner();
}


JavaScript is single-threaded. Any series of doSomethingQuicky(); should execute sequentially.

That is unless you're using some timer functions within doSomethingQuicky();. Without knowing what this function does, it's hard to advise.


var interval = setInterval(doSomethingQuickly, 500)

...

clearInterval(interval);

I don't know what the code is doing. JavaScript is single threaded so you shouldn't hit any problems. You also shouldn't sleep as it sleeps the only thread.


Using sleeps to wait for a function to return is always a bad idea. What if the slow function takes more time than predicted? What about time performance issues regarding the time spent idling?

Use promises instead:

// resolves immediatly to the string 'fast done'
const fast = new Promise(resolve => resolve('fast done'));

// resolves after 1 second to the string 'slow done'
const slow = new Promise(resolve => setTimeout(() => resolve('slow done'), 1000));

// resolves after 1 second to the array ['fast done', 'slow done'], then logs it for demo purposes
Promise.all([fast, slow]).then(console.log);

I think that Promise.all is exaclty what you're looking for. It resolves when all the promises that it gets as argument resolve, so you can pass it several functions with different execution time, and continue the code when all the functions have returned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜