0

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.

Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
dangerChihuahua007
  • 20,299
  • 35
  • 117
  • 206
  • 1
    Similar question: http://stackoverflow.com/questions/1141302/is-there-a-sleep-function-in-javascript – Schleis May 29 '13 at 21:17
  • Implementing a `wait()`, `pause()` or `sleep()` function is a big JS no-no. Whatever you're trying to achieve can likely be done with `setTimeout()` instead. – nnnnnn May 29 '13 at 21:18
  • What are you trying to achieve? You probably want to delay some code execution? For that see setTimeout and setInterval functions. – darma May 29 '13 at 21:18
  • What are you trying to do? Maybe `setInterval(function(){alert("Hello")},5000);` is the solution here. – acdcjunior May 29 '13 at 21:18
  • I am writing a javascript timer that lets you time how long functions run. I want to test it by writing a function that actually takes about 5 seconds. – dangerChihuahua007 May 29 '13 at 21:19
  • 1
    [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) –  May 29 '13 at 21:22
  • @DavidFaux: Then just do something like this: http://jsbin.com/igiyih/5/edit - the only thing that is required for the function you want to time is that it takes an argument `callback` which it calls when its done doing what it needs to do. – chrisbuchholz May 29 '13 at 21:32

6 Answers6

4

You cannot freeze the page without freezing the page.

You're trying to write a function that runs for 5 seconds straight.
Javascript runs on the UI thread, so the page will be frozen whenever any code runs.

In short, you can't do that.
You need to write asynchronous code, probably by using setTimeout().

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
3

you can use:

var t = setTimeout(function(){alert('done')},5000);
Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
2

If you want to wait 5 seconds before doing something, use setTimeout or setInterval. Otherwise, just eating up user time is a bad idea. In fact, the browsers will interrupt your script if it takes too long to do something, which your function most certainly will do.

John Fisher
  • 22,355
  • 2
  • 39
  • 64
2

(Ignoring for the moment why you shouldn't really do this...)

The reason your code is freezing the browser is that your while condition will never become true (for numSeconds of 1 or more).

The .getMilliseconds() method returns just the milliseconds part of the date, i.e., a number between 0 and 999. For numSeconds of 1 of more your end will always be greater than 999.

Try using .getTime() instead of .getMilliseconds(). Or if you don't care about supporting old browsers uses Date.now().

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
1

You could just use javascripts setTimeout instead of your own wait function.

setTimeout excecutes a function after a certain amount of time.

For example:

setTimeout(function(){}, 5000);

Would just wait 5 seconds.

See more at w3schools.

Nick Humrich
  • 14,905
  • 8
  • 62
  • 85
1

Strictly speaking, what you ask cannot be done because, as you've seen, the page will freeze while you wait.

As a practical matter, though, I suspect that what you really want is the something like this:

function waitawhile() {
    setTimeout( function() {
        alert('5 seconds has passed');
    }, 5000);
}
Arthur Dent
  • 785
  • 3
  • 7