What use is setInterval with a value of zero? [duplicate] - javascript

This question already has answers here:
Why is setTimeout(fn, 0) sometimes useful?
(19 answers)
Closed 9 years ago.
I have seen some JavaScript code that has the following:
var myFunc = function () {
timeout = setTimeout(myFunc, 0);
}
It seems that this would immediately recall the function.
What use case is there for this?

Read this.
In short, it "pauses" the JavaScript execution to let the rendering threads catch up. It gives the browser a chance to finish doing some none-JavaScript things that have been waiting to finish before attending to this new piece of JavaScript.

Related

Is there any affect of using set timeout with 0 seconds in the sequence of the program execution? [duplicate]

This question already has answers here:
Why is setTimeout(fn, 0) sometimes useful?
(19 answers)
Closed 2 years ago.
What should be the output of the following program and kindly explain the code as well.
console.log("first");
setTimeout(() => {
console.log("second");
}, 0);
console.log("third");
In this scenario, it should have the below output:
"first";
"third";
"second";
Detailed explanation is in the link: https://www.geekabyte.io/2014/01/javascript-effect-of-setting-settimeout.html
The output will be like this:
first third second
Reason: Actually this is the combination of both stack and the queue. Each statement will run in sequence but "SetTimeOut" will push the specific line in the stack within the queue that will be executed after the time. Although it has zero second, still due to stack, it will run after the next instruction.

setTimeout does not run when running while loop [duplicate]

This question already has answers here:
Why isn't setTimeout cancelling my loop?
(6 answers)
Closed 2 years ago.
I have this js snippet,
let checker={flag:0,dirtyFlag:false};
let i=0;
setTimeout(()=>{
console.log('value of checker updated');
checker.dirtyFlag=true;
checker.flag=1;
},2000)
while (true) {
console.log(i++);
if(checker.flag==1){
console.log(checker.dirtyFlag);
break;
}
}
but the code runs endless, whereas expected behavior is, it should stop after 2000ms.
how can i debug the above code.
The function that setTimeout calls is added to the call stack when the main call stack runs and completes. So, it will always run after the while loop (in your case it won't complete because you are in an endless loop)
You can see an example in the below image which shows how WEB API and Callback Queue works.

Javascript function timer. Not working as expected [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 5 years ago.
Problem: Hello i'm a little stuck as to why setTimeout() does not call the function specified after the time passed I've tried a few things but nothing seems to work
Solution: If anyone knows any other way to call a function after a specific time
Here is my code :
refreshStats: function(){
this.goldLabel.text = Math.floor(this.player.data.gold);
this.attackLabel.text = Math.floor(this.player.data.attack);
this.defenseLabel.text = Math.floor(this.player.data.defense);
if (this.player.questsDone.length > 0){
console.log(this.player.questsDone)
this.bpText.text = this.player.questsDone[this.player.questsDone.length-1];
setTimeout(this.FadeConsoleText(), 5000);
}
},
FadeConsoleText: function(){
console.log("log");
},
Current output:
"Quest"
"a"
wanted Solution output:
"Quest"
(wait then call function)
"a"
Thank you in advanced
You want to pass the function, not what the function returns to setTimeout:
setTimeout(this.FadeConsoleText, 5000);

Javascript: How to randomly change background ? [duplicate]

This question already has answers here:
Why is setTimeout executing immediately? [duplicate]
(4 answers)
Closed 8 years ago.
I can't find out where is the problem on this code:
<script>
var colori = ['ffffff', 'ffbf00', 'ff4000', '4d79ff', '00c0ff', '00ff3f', 'ff7a4d', '00ff40', 'bf00ff', 'ff0040', 'ffd34d', 'a52a2a', 'bf00ff', '47adcf', '2a82a0' ];
var i;
function CambiaColore(){
i = Math.floor(Math.random() * colori.length);
var colore = '#'+colori[i];
document.body.style.background=colore;
setTimeout(CambiaColore(),2000);
};
CambiaColore();
</script>
Can somebody explain me what's wrong?
setTimeout(CambiaColore(),2000); wrong
setTimeout( CambiaColore, 2000 ); right
Explanation:
SetTimeout's first argument should be function. And you got it. But brackets after function name means its execution (my bad english, ya). Thus in your case it comes to recursion and that's all. Maximum stack calls, error.. So I as understood, you don't need to execute func there but postpone its execution. Correct way to do this I showed above.
Another possibility is to wrap it in an anonymus function:
setTimeout ( function(){ CambiaColore() } , 2000 ) ;

setInterval and setTimeout don't seem to work with a console.log [duplicate]

This question already has answers here:
setTimeout ignores timeout? (Fires immediately) [duplicate]
(3 answers)
Closed 8 years ago.
I am answering my own question in the hopes it will solve someone else's headache:
I could not get the following code to work:
function givePosition(){
var place = $('#two').position();
console.log(place);
}
window.setInterval(givePosition(), 50);
The solution to my problem was to give the function name as a parameter without parentheses, like so:
window.setInterval(functionName, 50);
instead of:
window.setInterval(functionName(), 50)

Categories