Matt
Matt

Reputation: 2896

is there a sleep() workaround for JavaScript?

So that I could use a loop, and have it pause, say 250 ms after each iteration? [ without setTimeout or setInterval?]

Edit: Ok, so I have a for loop that uses AJAX to post data ($.ajax()) . It has lots of vars in it, like data: 'uid=' + uid, + '&othervars=' + morevars etc, and each loop, the variables change.. so If put that [huge] part into a function() {} [Inside of a setTimeout(), using a counter in the loop to increment the timeout], when it executes, will the actual value be in place where I put the vars?

Upvotes: 1

Views: 209

Answers (2)

Naftali
Naftali

Reputation: 146310

You will need to use one of javascript's timer functions (setTimeout or setInterval).

There is no real way around them.

Upvotes: 0

Matti Virkkunen
Matti Virkkunen

Reputation: 65126

Current browser implementations are essentially single-threaded, so sleeping in your code will likely block the entire UI. It's not what you want, and just plain isn't how JS works. Refactor your code so that you can use setTimeout.

Upvotes: 8

Related Questions