Last active
October 1, 2020 11:10
-
-
Save jastisriradheshyam/c28cdadaf0ffa44e326269d3c52d1ef7 to your computer and use it in GitHub Desktop.
Its a synchronous sleep function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 | |
*/ |
Author
jastisriradheshyam
commented
Oct 29, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment