Use PHP functions in JavaScript

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;}
external links: original PHP docs | raw js source

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.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
Sep 8th Permalink

q  We've always looked at php.js as this big resource for anyone to just take what they want. And leave what not. But now that php.js is becoming pretty huge, we probably should do more to protect our users against themselves.

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

Gravatar
Brett Zamir
Aug 3rd Permalink

q  @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.)

Gravatar
Robert K
Aug 3rd Permalink

q  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.");
}


Gravatar
Brett Zamir
Aug 3rd Permalink

q  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.

Gravatar
Roger
Aug 3rd Permalink

q  usleep for NodeJS implemented with an active loop!! WTF!!! That is the shittiest code I have seen in ages! Give up already because you obviously don't have a clue!

Gravatar
Jimbastard
Aug 3rd Permalink

q  is this a joke?


Contribute a New function

More functions

In this category

constant
define
defined
die
exit
pack
php_strip_whitespace
sleep
time_nanosleep
time_sleep_until
uniqid
» usleep

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !