Javascript: How to randomly change background ? [duplicate] - javascript

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

Related

setTimeout inside a function [duplicate]

This question already has answers here:
setTimeout(): If not defined in EcmaScript spec, where can I learn how it works?
(2 answers)
Closed 6 months ago.
function delay2sec(){
let name = "abc"
setTimeout(function () {
name = "xyz"
}, 0)
return name
}
console.log(delay2sec())
The output is abc.
Can someone explain why it is not reassigning the name to 'xyz'?
In order to understand this behavior, you need to understand how the event loop works:
Your setTimeout adds an event to the event loop, which will be executed after the current function. Your return statement is evaluated before the current function ends, so the return evaluates the value before the change.

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

how to use recursive setTimeout from within a method [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 8 years ago.
Im trying to use setTimeout() for a recursive function, inside a method. I'm assuming I'm getting something with the 'this' scope wrong though. Here's an example.
(function(scope){
function gamePlay(){
// this.initialize();
}
gamePlay.prototype.droidMovement = function(){
console.log('droid is moving');
this.droidShoot();
}
gamePlay.prototype.droidShoot = function(){
console.log('droidShoot() has been fired');
setTimeout(this.droidShoot,1000);
}
scope.gamePlay = gamePlay;
})(window);
var game = new gamePlay();
game.droidMovement();
Ive tried the setTimeout 3 ways.
//this way repeats the function 2 times then stops
setTimeout(this.droidShoot,1000);
//this gives me an error
setTimeout(function(){this.droidShoot()},1000);
//this fires the function in a loop but doesnt wait the second
setTimeout(this.droidShoot(),1000);
Can anyone please explain what I'm doing wrong? thanks so much in advance.
You may want to remove the setTimeout from within the definition of droidShoot and replace setTimeout(this.droidShoot, 1000) with setInterval(this.droidShoot, 1000.
setInterval will automatically call this.droidShoot repeatedly at a 1s interval

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)

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

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.

Categories