[ACCEPTED]-Sleep in javascript - no setTimeout-javascript

Accepted answer
Score: 19

From phpied.com:

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

0

Score: 2

I don't think you can. You'll probably have 1 to

do_fn1();
window.setTimeout(do_fn2, 5000);
Score: 2

Two thoughts:

first of all why not wrap up 25 all of the post delay statements into a 24 wrapper function

 var postDelayFunc = function(){
   dosomething();
   dosomethingelse();
   onemorething();
 }

then in your code pass this 23 function as the parameter to setTimeout.

 //your code
 dofunc1();
 setTimeout(postDelayFunc, 1000);

Alternatively 22 take a look at jQuery deferred: http://msdn.microsoft.com/en-us/scriptjunkie/gg723713, although 21 you will probably end up writing very similar 20 code.

One thing struck me though about your 19 responses to other answers and possibly 18 where the confusion arises. I think you 17 are looking at your function and seeing 16 a single thread you just want to hold up 15 for a while before carrying on.

You should 14 not do this though in javascript as it ties 13 up the entire browser and will annoy the 12 hell out of users. Instead what you are 11 in effect doing when you use setTimeout, is 10 indicating that when the timeout expires 9 another thread will pick up and execute 8 the passed in function.

As soon as the timeout 7 has been set, the executing thread will 6 continue with the next line (which is why 5 you think the timeout isn't working). What 4 you probably need to do, is set the timeout, and 3 put ALL the post-execution steps into the 2 function handed off to the timer as indicated 1 above.

Score: 0

Saying they all don't work without an example 2 is big call because I'm sure they probably 1 do.

How about this,

do_fn1();
setTimeout(do_fn2, 5000);
Score: 0

All those setTimeout answers here don't 1 work!

Of course they do:

function a() {
  alert("I'm pretty sure...");
}

function b() {
  alert("...that they work just fine.");
}

a();
setTimeout(b, 5000);
Score: 0

Another hack I will probably use, however 2 personally I would not recommend it.
Check 1 out here http://jsfiddle.net/S6Ks8/1/

function parseSleeps(func){
    var fdef = func.toString();

    var fbody = fdef.match(/\{([\s\S]*)\}/)[1].split(/sleep\(.*?\)\;?/);
    var sleeps = fdef.match(/sleep\((.*?)\)/g);
    var fargs = fdef.match(/\(([\s\S]*?)\)/)[1];

    var fbodyNew = [];
    var times = [];
    fbodyNew.push(fbody.shift(), '\n');
    for(var i = 0; sleeps && i < sleeps.length; i++){
        var sec = sleeps[i].match(/\d+/)[0];
        times.push(sec);
        fbodyNew.push('setTimeout(function(){\n');
        fbodyNew.push(fbody.shift(), '\n');
    }

    while(times.length){
        var sec = times.pop();
        fbodyNew.push('}, ', sec, ');\n');
    }

    return new Function(fargs, fbodyNew.join(''));
}

// Your code from here

function a(str1, str2){
    alert(str1);
    sleep(3000);
    alert(str2);
}

var func = parseSleeps(a);
func('here', 'there');
Score: 0

The smartest way would be to have something 3 like

function a() {
    // Do stuff
    setTimeout(b, 42)
}

function b() {
    // Do other stuff delayed
}    

Never "block" any Threads in JS - if 2 you think you have to do there is definately 1 a "cleaner" way to do achieve your aim.

More Related questions