简体   繁体   中英

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?

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.

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

There is no real way around them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM