Skip to content

Instantly share code, notes, and snippets.

@jastisriradheshyam
Last active October 1, 2020 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jastisriradheshyam/c28cdadaf0ffa44e326269d3c52d1ef7 to your computer and use it in GitHub Desktop.
Save jastisriradheshyam/c28cdadaf0ffa44e326269d3c52d1ef7 to your computer and use it in GitHub Desktop.
Its a synchronous sleep function.
/*
Usage:
-------------------------------------------------
var syn_timeout = require('synchronous_timeout');
//some code
await syn_timeout.pause(300);
//some code
-------------------------------------------------
if defined and used in a same file:
-------------------------------------------------
//some code
await pause(300);
//some code
-------------------------------------------------
*/
var pause = function(milliseconds) {
var dt = new Date();
while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
}
// BETTER THAN PAUSE
var sleep = function (sleep_time_ms) {
return new Promise(resolve => setTimeout(resolve, sleep_time_ms));
}
/*
Credits: Nathan
Site: https://stackoverflow.com/questions/4122268/using-settimeout-synchronously-in-javascript
*/
@jastisriradheshyam
Copy link
Author

jastisriradheshyam commented Oct 29, 2018

// wait ms milliseconds
function wait(ms) {
  return new Promise(r => setTimeout(r, ms));
}
// Same as above function sleep but better representation and meaning

reference :
https://developers.google.com/web/fundamentals/primers/async-functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment