JavaScript usleep
Delay for a given number of micro seconds
1 2 3 4 56 7 8 9 1011 12 13 14 1516 | function usleep (microseconds) { // Delay for a given number of micro seconds // // version: 1008.1718 // discuss at: http://phpjs.org/functions/usleep // + original by: Brett Zamir (http://brett-zamir.me) // % note 1: For study purposes. Current implementation could lock up the user's browser. // % note 1: Consider using setTimeout() instead. // % note 2: Note that this function's argument, contrary to the PHP name, does not // % note 2: start being significant until 1,000 microseconds (1 millisecond) // * example 1: usleep(2000000); // delays for 2 seconds // * returns 1: true var start = new Date().getTime(); while (new Date() < (start + microseconds/1000)) {} return true;} |
Examples
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have usleep goodness in JavaScript.
@Robert: Maybe some people who would not read the notes of our function would also not look at the content of the alert in your function and think the alert itself was meant to give the needed pause--and maybe it actually would!! :)
Seriously though, Kevin, I think I agree now that these are candidates for moving to experimental judging by the reaction here, and Robert's rationale does make sense. Locking up the browser is not just a problem for the dev who tries it--some may put it as a condition in some infrequent code and annoy their users.
Frankly I don't care so much if people judge our coding competence at php.js based on misinformation since I'm rather used to such comments (nothing easier to prop yourself up than bashing others using or adapting a simple language), but we should do more to protect people from themselves, even if it means a note is not enough and we need to put up more fences for others who may actually have a use or who just like to compare languages. Sigh... (Thankfully Kevin at least had the insight earlier to start an experimental section for such cases--I just leave people to the wolves I guess.)
Brett, thanks for not reacting too severely to the trolling of others. That said, I do think they are a bit justified in their reactions here.
JS is simply not designed to do synchronous pausing. By including code like this in your library - code that should simply *never* be used (except in extremely rare cases) - you are asking people to dismiss php.js as a library built by people that neither know nor care how JS works, and mislead new developers into thinking, "well, php.js does it, so it can't be _that_ bad, right?" To which the answer is, "well, actually, it is that bad. You should never, ever do this. Use setTimeout instead." Thus, to answer your question, here's how you should implement this method:
function usleep() {
alert("The javascript programming model does not support synchronous pausing. Please refer to https://developer.mozilla.org/en/window.setTimeout for information about how timers and delays in javascript work.");
}
And how would you propose a synchronous pause in JavaScript as equivalent to PHP's function, as is the aim of this project? As the notes say, this is for study purposes. And this project is not only for NodeJS, btw.


Kevin van Zonneveld
Sep 8th
I will start moving more functions to experimental.
Brett & Theriault, if you see bad function that are already commented with things like: don't ever use in production.. Please move them to experimental as well.
Let's go for a higher quality lesser quantity main repo, and leave the thought exercises in _experimental