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


Kevin van Zonneveld
2 Feb '09