1

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?

6
  • You can create your own sleep() subroutine that takes care of all those details for you, and you could even support a parameter (I suggest making it optional and default to 1 second if no parameter was provided). Commented Jun 29, 2011 at 3:13
  • How? I don't know what subroutine is Commented Jun 30, 2011 at 15:16
  • Lots of results here on SO already: stackoverflow.com/search?q=javascript+sleep Commented Jun 30, 2011 at 16:51
  • possible duplicate of Sleep in javascript - no setTimeout Commented Jun 30, 2011 at 16:53
  • why don't you want to use setTimeout or setInterval? Commented Jun 30, 2011 at 16:53

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

... or event handlers. It's tempting to add a sleep just to let the DOM finish loading ;-)
0

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

There is no real way around them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.