-1

I'm new to JavaScript from Java, and I'm trying to implement a sleep(millis) function. Basically I need to make a request to an endpoint, and if it returns an error, wait a while and make the request again...

I've found JavaScript's setTimeout() function, but it has a strange (for me) behaviour, because it seems that the code is still running, and I don't want to do anything but wait...

After a little research I've found this function here:

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

With this function I achieve exactly what I want, but I understand it may not be the proper way to do it and I didn't find any other solutions like this... So, I'd like to know, in your opinion, is this:

  • A perfectly valid solution?
  • A not too bad workaround?
  • Just painful to your eyes?
BenMorel
  • 34,448
  • 50
  • 182
  • 322
MikO
  • 18,243
  • 12
  • 77
  • 109
  • 1
    I'd much rather use `setTimeout` to schedule my runs than eat up cpu resources looping like that. – NilsH May 08 '13 at 12:02
  • 1
    Check this http://stackoverflow.com/questions/951021/what-do-i-do-if-i-want-a-javascript-version-of-sleep – 999k May 08 '13 at 12:03
  • 1
    You can do this in proper and javascript way. Make ajax request to endpoint and on getting response, handle it like this. If error, give same function as `setTimeout`'s arg. otherwise `clearTimeout` with your timeoutId. – smitrp May 08 '13 at 12:08

4 Answers4

1

Well, opinions will vary. I can understand you though, JavaScript doesn't have a real sleep function like Java or any other language (like Perl, Bash, PHP, etc)

http://devcheater.com/ shows multiple variations, but also check out these Stackoverflow topics:

What is the JavaScript version of sleep()? What's the best way to implement a sleep function JavaScript? How to make a REAL sleep() in JavaScript?

Community
  • 1
  • 1
ChrisH
  • 1,283
  • 1
  • 9
  • 22
1

The problem in this code is that is stays busy all the time until the code finishes. This may cause a warning dialog in many browsers, and it also slows the page down.

The dialog

Here I would still prefer to use the setTimeout function. The syntax is quite simple:

setTimeout("codeToBeExecuted()", milliseconds);

You can simplify it by saving the code after sleeping in a function, and passing the needed data in global variables or like this:

setTimeout('whenDone(' + numericData + ', "' + stringData + '")', milliseconds);

JSFiddle

PurkkaKoodari
  • 6,703
  • 6
  • 37
  • 58
1

You say you don't like setTimeout, but basically, that's what you need to do.

function myFunc() {
    // Do the work
    setTimeout(myFunc, sleepInterval);
}

(Assuming you're trying to create a repeating schedule, if not, call your other function rather than myFunc again).

You can (and probably should) of course add some termination logic as well.

Then start your work with myFunc(). You could also use setInterval, but it can cause some problems if your intervals start to overlap.

Regarding your solution, I select option 3...

NilsH
  • 13,705
  • 4
  • 41
  • 59
1

There is no sleep function in Javascript, and you should consider using window.setTimeout to call the next operation. I certainly would not burn cycles in a for loop. But if you must wrap it in a function named sleep then here is an example to give you an idea.

function sleep(callBack, milliSecs) {
    setTimeout(callBack, milliSecs);
}


function first() {
    console.log("first");
}

function second() {
    console.log("second");
}

first();
sleep(second, 5000);

On jsfiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79