I want to write a javascript function that takes very close to 5 seconds to run. How can I make that guarantee?
I have tried
function wait(numSeconds) { var end = new Date().getMilliseconds() + numSeconds * 1000; while (new Date().getMilliseconds() <= end) {} } but this just crashes the page.
wait(),pause()orsleep()function is a big JS no-no. Whatever you're trying to achieve can likely be done withsetTimeout()instead.setInterval(function(){alert("Hello")},5000);is the solution here.