-1

Is there any way to pause our main thread in jQuery for a minutes/seconds.

Let take a example.

I need to print 25 elements, then after few second, I need print 25 to 49 elements? Similar to sleep mode in Java. Is there any method which stop processing?

http://jsfiddle.net/ZxhKh/1/

for(var i=0;i<50;i++ ){
console.log("---------------"+i);
    if(i==25){
        //stop thread for 1 min ?
    }
}

Thanks

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
user2648752
  • 2,123
  • 5
  • 21
  • 29
  • 1
    There is only one thread in js. If you sleep it - the browser will hang – zerkms Nov 20 '13 at 00:11
  • I know setTimeout ..But i know want to use settimeout – user2648752 Nov 20 '13 at 00:20
  • Uh what? Are you saying you don't know how `setTimeout` works? You could have a look at the documentation: https://developer.mozilla.org/en-US/docs/Web/API/Window.setTimeout. – Felix Kling Nov 20 '13 at 00:22
  • well, you could do it with a while loop that checks the current time, however there are limits to how long the browser will let you do that before it stops it as an infinite loop. And it's a terrible thing to do. – Kevin B Nov 20 '13 at 00:40

1 Answers1

1

I think you're looking at setTimeout, not threads.

for(var i=0;i<=25;i++ ){
       console.log("---------------"+i);  
}
setTimeout(function() {
    for(var i=25;i<50;i++ ){
       console.log("---------------"+i);  
    }
}, 1000); // print after waiting one second

The equivalent of sleep would not do what you would want it to do. In a console application, there is seemingly no difference because the user is not doing anything while the program is 'sleeping'. However if you were to sleep the browser thread, it would of course hang the application.