Use PHP functions in JavaScript

JavaScript time_sleep_until

Make the script sleep until the specified time

1
2
3
4
56
7
8
9
1011
12
13
14
function time_sleep_until (timestamp) {
    // Make the script sleep until the specified time  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/time_sleep_until    // +   original by: Brett Zamir (http://brett-zamir.me)
    // %          note: For study purposes. Current implementation could lock up the user's browser.
    // %          note: Expects a timestamp in seconds, so DO NOT pass in a JavaScript timestamp which are in milliseconds (e.g., new Date()) or otherwise the function will lock up the browser 1000 times longer than probably intended.
    // %          note: Consider using setTimeout() instead.
    // *     example 1: time_sleep_until(1233146501) // delays until the time indicated by the given timestamp is reached    // *     returns 1: true
    while (new Date() < timestamp*1000) {}
    return true;
}
external links: original PHP docs | raw js source

Examples

Running

1
time_sleep_until(1233146501) // delays until the time indicated by the given timestamp is reached

Should return

1
true

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 time_sleep_until 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
2 Feb '09 Permalink

q  @ Brett Zamir: It's probably possible to detect for this kind of input since the difference between good &amp; bad input is at least 3 digits. Maybe we should sanitize it and save our users the trouble.

Gravatar
Brett Zamir
2 Feb '09 Permalink

q  A quick warning about this function (I'll try to add to SVN later)... This works in the PHP style of expecting a timestamp in seconds--NOT IN MILLISECONDS! So, don't try to pass in a raw JavaScript timestamp (like new Date())--which are in milliseconds, or else it will lock up the browser probably a thousand times longer than you intended! (No doubt you'd restart before that anyways (!), but, it's no fun to lock up the browser...)


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 !