James
James

Reputation: 43647

Freeze script on some seconds

$(obj).each(function()
// 1) freeze
// 2) then do something
});

How do I freeze the script on some seconds?

Upvotes: 1

Views: 4284

Answers (3)

Guffa
Guffa

Reputation: 700372

You don't freeze the script, that doesn't work well in an enviromment like a browser, you use a timeout to schedule code to start at a later time.

The trick with scheduling code in a loop is to schedule the code to run at differnt time in the future, e.g. the first code to run after 1000 ms., the second code to run after 2000 ms., and so on:

$(obj).each(function(i) {
  window.setTimeout(function(){
    // do something
  }, (i + 1) * 1000);
});

Upvotes: 2

brad
brad

Reputation: 32345

I'm assuming you want something like sleep ? You use the function setTimeout to call a function after a certain amount of time. So:

$(obj).each(function()
  setTimeout(someFunction, 1000);
});

to invoke someFunction after 1000ms, or as Andy mentioned you can just define an anonymous function to be called.

Upvotes: 3

Andy
Andy

Reputation: 30135

$(obj).each(function()
   setTimeout(function(){
      //do stuff
   },2000);
});

Upvotes: 5

Related Questions