Helper function to make `setTimeout` synchronousAsync, callbacks, and closuresProcessing image blocks using a task runnerAm I using Q the right way or is there a better way to do what I'm doing?Safer way to wait amount of timeRemoving duplicates from an arrayCallback of setTimeout function when performing synchronous actionsSynchronous loop with internal asynchronous callsReplacing “no-js” with “js” class in the body element properlyFetching user details, posts, and comments using promises in ExpressJSSpawning multiple child processes to preview Vue.js projects
"which" command doesn't work / path of Safari?
Download, install and reboot computer at night if needed
Draw simple lines in Inkscape
When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?
Are tax years 2016 & 2017 back taxes deductible for tax year 2018?
Circuitry of TV splitters
Motorized valve interfering with button?
How to use Pandas to get the count of every combination inclusive
Can a German sentence have two subjects?
What is the command to reset a PC without deleting any files
Extreme, but not acceptable situation and I can't start the work tomorrow morning
Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?
What Brexit solution does the DUP want?
How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?
How old can references or sources in a thesis be?
Concept of linear mappings are confusing me
Could a US political party gain complete control over the government by removing checks & balances?
A Journey Through Space and Time
Modification to Chariots for Heavy Cavalry Analogue for 4-armed race
Patience, young "Padovan"
Should I join an office cleaning event for free?
What typically incentivizes a professor to change jobs to a lower ranking university?
What is GPS' 19 year rollover and does it present a cybersecurity issue?
Is there really no realistic way for a skeleton monster to move around without magic?
Helper function to make `setTimeout` synchronous
Async, callbacks, and closuresProcessing image blocks using a task runnerAm I using Q the right way or is there a better way to do what I'm doing?Safer way to wait amount of timeRemoving duplicates from an arrayCallback of setTimeout function when performing synchronous actionsSynchronous loop with internal asynchronous callsReplacing “no-js” with “js” class in the body element properlyFetching user details, posts, and comments using promises in ExpressJSSpawning multiple child processes to preview Vue.js projects
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I'm making a helper function to make setTimeout
synchronous. Currently, it is asynchronous so other code runs before the timeout is done.
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
eval("(" + func + ")();");
sync(true);
, ms);
return;
eval("(" + callback + ")();");
)();
func
is a function that should be run after the specified timeout.ms
specifies how many milliseconds to wait before runningfunc
.callback
is the function that runs after thefunc
is done running.
Is there a way to get rid of those eval
statements? I've read that eval
is evil. I'm also wondering if I can do this without a nested function. If there are any other ways to improve this code then please say so.
javascript
$endgroup$
add a comment |
$begingroup$
I'm making a helper function to make setTimeout
synchronous. Currently, it is asynchronous so other code runs before the timeout is done.
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
eval("(" + func + ")();");
sync(true);
, ms);
return;
eval("(" + callback + ")();");
)();
func
is a function that should be run after the specified timeout.ms
specifies how many milliseconds to wait before runningfunc
.callback
is the function that runs after thefunc
is done running.
Is there a way to get rid of those eval
statements? I've read that eval
is evil. I'm also wondering if I can do this without a nested function. If there are any other ways to improve this code then please say so.
javascript
$endgroup$
add a comment |
$begingroup$
I'm making a helper function to make setTimeout
synchronous. Currently, it is asynchronous so other code runs before the timeout is done.
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
eval("(" + func + ")();");
sync(true);
, ms);
return;
eval("(" + callback + ")();");
)();
func
is a function that should be run after the specified timeout.ms
specifies how many milliseconds to wait before runningfunc
.callback
is the function that runs after thefunc
is done running.
Is there a way to get rid of those eval
statements? I've read that eval
is evil. I'm also wondering if I can do this without a nested function. If there are any other ways to improve this code then please say so.
javascript
$endgroup$
I'm making a helper function to make setTimeout
synchronous. Currently, it is asynchronous so other code runs before the timeout is done.
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
eval("(" + func + ")();");
sync(true);
, ms);
return;
eval("(" + callback + ")();");
)();
func
is a function that should be run after the specified timeout.ms
specifies how many milliseconds to wait before runningfunc
.callback
is the function that runs after thefunc
is done running.
Is there a way to get rid of those eval
statements? I've read that eval
is evil. I'm also wondering if I can do this without a nested function. If there are any other ways to improve this code then please say so.
javascript
javascript
edited Jul 4 '17 at 17:26
Jamal♦
30.6k11121227
30.6k11121227
asked Nov 14 '16 at 22:59
ericw31415ericw31415
11315
11315
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
The Eval
Eval is indeed, the monster. There are plenty of reasons why you should not use it, although there are some situations where you are forced to use it. For sure, this is not the requirement in yours situation. If func
is a function, you can call it using .apply
.
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
func.apply(func);
sync(true);
, ms);
return;
callback.apply(callback);
)();
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
Or event without apply:
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
func();
sync(true);
, ms);
return;
callback();
)();
The synchronous
You need to know, that trying to make setTimeout()
sync means that you have some very bad design idea behind. You should not do it and you will not be able to make it really sync without killing performance of the browser and UX of your app.
I believe that you would like to create sync delay in your application. You are not the first who wants to do it (most of beginners or people with background from different language). You can take a look at answers to this question from stackoverflow: https://stackoverflow.com/questions/6921895/synchronous-delay-in-code-execution
From the answer to linked question (by OverZealous):
JavaScript is a single-threaded language. You cannot combine
setTimeout and synchronous processing. What will happen is, the timer
will lapse, but then the JS engine will wait to process the results
until the current script completes.
One reason people are trying to take advantage of sync delays is returning result from async code. This is not good strategy in JS. If you would like to return async results just use callbacks or more likely - Promises or Observables.
$endgroup$
$begingroup$
I don't really understand what.apply()
does. Can you explain? I've tried this page, but that's not very clear.
$endgroup$
– ericw31415
Nov 14 '16 at 23:08
$begingroup$
Check my updated answer.
$endgroup$
– rzelek
Nov 14 '16 at 23:14
$begingroup$
So are you saying that my function is flawed?
$endgroup$
– ericw31415
Nov 15 '16 at 2:05
add a comment |
$begingroup$
There's a simpler solution if you are using nodejs
code
await new Promise(done => setTimeout(() => done(), 180000));
/codeawait new Promise(done => setTimeout(() => done(), 180000));
New contributor
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f147039%2fhelper-function-to-make-settimeout-synchronous%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
The Eval
Eval is indeed, the monster. There are plenty of reasons why you should not use it, although there are some situations where you are forced to use it. For sure, this is not the requirement in yours situation. If func
is a function, you can call it using .apply
.
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
func.apply(func);
sync(true);
, ms);
return;
callback.apply(callback);
)();
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
Or event without apply:
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
func();
sync(true);
, ms);
return;
callback();
)();
The synchronous
You need to know, that trying to make setTimeout()
sync means that you have some very bad design idea behind. You should not do it and you will not be able to make it really sync without killing performance of the browser and UX of your app.
I believe that you would like to create sync delay in your application. You are not the first who wants to do it (most of beginners or people with background from different language). You can take a look at answers to this question from stackoverflow: https://stackoverflow.com/questions/6921895/synchronous-delay-in-code-execution
From the answer to linked question (by OverZealous):
JavaScript is a single-threaded language. You cannot combine
setTimeout and synchronous processing. What will happen is, the timer
will lapse, but then the JS engine will wait to process the results
until the current script completes.
One reason people are trying to take advantage of sync delays is returning result from async code. This is not good strategy in JS. If you would like to return async results just use callbacks or more likely - Promises or Observables.
$endgroup$
$begingroup$
I don't really understand what.apply()
does. Can you explain? I've tried this page, but that's not very clear.
$endgroup$
– ericw31415
Nov 14 '16 at 23:08
$begingroup$
Check my updated answer.
$endgroup$
– rzelek
Nov 14 '16 at 23:14
$begingroup$
So are you saying that my function is flawed?
$endgroup$
– ericw31415
Nov 15 '16 at 2:05
add a comment |
$begingroup$
The Eval
Eval is indeed, the monster. There are plenty of reasons why you should not use it, although there are some situations where you are forced to use it. For sure, this is not the requirement in yours situation. If func
is a function, you can call it using .apply
.
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
func.apply(func);
sync(true);
, ms);
return;
callback.apply(callback);
)();
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
Or event without apply:
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
func();
sync(true);
, ms);
return;
callback();
)();
The synchronous
You need to know, that trying to make setTimeout()
sync means that you have some very bad design idea behind. You should not do it and you will not be able to make it really sync without killing performance of the browser and UX of your app.
I believe that you would like to create sync delay in your application. You are not the first who wants to do it (most of beginners or people with background from different language). You can take a look at answers to this question from stackoverflow: https://stackoverflow.com/questions/6921895/synchronous-delay-in-code-execution
From the answer to linked question (by OverZealous):
JavaScript is a single-threaded language. You cannot combine
setTimeout and synchronous processing. What will happen is, the timer
will lapse, but then the JS engine will wait to process the results
until the current script completes.
One reason people are trying to take advantage of sync delays is returning result from async code. This is not good strategy in JS. If you would like to return async results just use callbacks or more likely - Promises or Observables.
$endgroup$
$begingroup$
I don't really understand what.apply()
does. Can you explain? I've tried this page, but that's not very clear.
$endgroup$
– ericw31415
Nov 14 '16 at 23:08
$begingroup$
Check my updated answer.
$endgroup$
– rzelek
Nov 14 '16 at 23:14
$begingroup$
So are you saying that my function is flawed?
$endgroup$
– ericw31415
Nov 15 '16 at 2:05
add a comment |
$begingroup$
The Eval
Eval is indeed, the monster. There are plenty of reasons why you should not use it, although there are some situations where you are forced to use it. For sure, this is not the requirement in yours situation. If func
is a function, you can call it using .apply
.
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
func.apply(func);
sync(true);
, ms);
return;
callback.apply(callback);
)();
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
Or event without apply:
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
func();
sync(true);
, ms);
return;
callback();
)();
The synchronous
You need to know, that trying to make setTimeout()
sync means that you have some very bad design idea behind. You should not do it and you will not be able to make it really sync without killing performance of the browser and UX of your app.
I believe that you would like to create sync delay in your application. You are not the first who wants to do it (most of beginners or people with background from different language). You can take a look at answers to this question from stackoverflow: https://stackoverflow.com/questions/6921895/synchronous-delay-in-code-execution
From the answer to linked question (by OverZealous):
JavaScript is a single-threaded language. You cannot combine
setTimeout and synchronous processing. What will happen is, the timer
will lapse, but then the JS engine will wait to process the results
until the current script completes.
One reason people are trying to take advantage of sync delays is returning result from async code. This is not good strategy in JS. If you would like to return async results just use callbacks or more likely - Promises or Observables.
$endgroup$
The Eval
Eval is indeed, the monster. There are plenty of reasons why you should not use it, although there are some situations where you are forced to use it. For sure, this is not the requirement in yours situation. If func
is a function, you can call it using .apply
.
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
func.apply(func);
sync(true);
, ms);
return;
callback.apply(callback);
)();
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
Or event without apply:
function syncSetTimeout(func, ms, callback)
(function sync(done)
if (!done)
setTimeout(function()
func();
sync(true);
, ms);
return;
callback();
)();
The synchronous
You need to know, that trying to make setTimeout()
sync means that you have some very bad design idea behind. You should not do it and you will not be able to make it really sync without killing performance of the browser and UX of your app.
I believe that you would like to create sync delay in your application. You are not the first who wants to do it (most of beginners or people with background from different language). You can take a look at answers to this question from stackoverflow: https://stackoverflow.com/questions/6921895/synchronous-delay-in-code-execution
From the answer to linked question (by OverZealous):
JavaScript is a single-threaded language. You cannot combine
setTimeout and synchronous processing. What will happen is, the timer
will lapse, but then the JS engine will wait to process the results
until the current script completes.
One reason people are trying to take advantage of sync delays is returning result from async code. This is not good strategy in JS. If you would like to return async results just use callbacks or more likely - Promises or Observables.
edited May 23 '17 at 12:41
Community♦
1
1
answered Nov 14 '16 at 23:06
rzelekrzelek
1814
1814
$begingroup$
I don't really understand what.apply()
does. Can you explain? I've tried this page, but that's not very clear.
$endgroup$
– ericw31415
Nov 14 '16 at 23:08
$begingroup$
Check my updated answer.
$endgroup$
– rzelek
Nov 14 '16 at 23:14
$begingroup$
So are you saying that my function is flawed?
$endgroup$
– ericw31415
Nov 15 '16 at 2:05
add a comment |
$begingroup$
I don't really understand what.apply()
does. Can you explain? I've tried this page, but that's not very clear.
$endgroup$
– ericw31415
Nov 14 '16 at 23:08
$begingroup$
Check my updated answer.
$endgroup$
– rzelek
Nov 14 '16 at 23:14
$begingroup$
So are you saying that my function is flawed?
$endgroup$
– ericw31415
Nov 15 '16 at 2:05
$begingroup$
I don't really understand what
.apply()
does. Can you explain? I've tried this page, but that's not very clear.$endgroup$
– ericw31415
Nov 14 '16 at 23:08
$begingroup$
I don't really understand what
.apply()
does. Can you explain? I've tried this page, but that's not very clear.$endgroup$
– ericw31415
Nov 14 '16 at 23:08
$begingroup$
Check my updated answer.
$endgroup$
– rzelek
Nov 14 '16 at 23:14
$begingroup$
Check my updated answer.
$endgroup$
– rzelek
Nov 14 '16 at 23:14
$begingroup$
So are you saying that my function is flawed?
$endgroup$
– ericw31415
Nov 15 '16 at 2:05
$begingroup$
So are you saying that my function is flawed?
$endgroup$
– ericw31415
Nov 15 '16 at 2:05
add a comment |
$begingroup$
There's a simpler solution if you are using nodejs
code
await new Promise(done => setTimeout(() => done(), 180000));
/codeawait new Promise(done => setTimeout(() => done(), 180000));
New contributor
$endgroup$
add a comment |
$begingroup$
There's a simpler solution if you are using nodejs
code
await new Promise(done => setTimeout(() => done(), 180000));
/codeawait new Promise(done => setTimeout(() => done(), 180000));
New contributor
$endgroup$
add a comment |
$begingroup$
There's a simpler solution if you are using nodejs
code
await new Promise(done => setTimeout(() => done(), 180000));
/codeawait new Promise(done => setTimeout(() => done(), 180000));
New contributor
$endgroup$
There's a simpler solution if you are using nodejs
code
await new Promise(done => setTimeout(() => done(), 180000));
/codeawait new Promise(done => setTimeout(() => done(), 180000));
New contributor
New contributor
answered 11 mins ago
Anoop GeorgeAnoop George
101
101
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f147039%2fhelper-function-to-make-settimeout-synchronous%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown