How can I set delay for running functions [duplicate] - javascript

This question already has answers here:
How to create javascript delay function [duplicate]
(3 answers)
Closed 6 years ago.
I have javascript for call 2 function for example:
system.library.myscript.function1();
system.library.myscript.function2();
I would like to set delay before run function2 for example 20 seconds.
Highly appreciated for anything helps.

Use setTimeout() function:
setTimeout(function(){
alert("Hello");
}, 3000);
:)

Related

i cant pass a parameter to a function using javascript [duplicate]

This question already has answers here:
How can I pass a parameter to a setTimeout() callback?
(29 answers)
Closed 2 years ago.
i am trying to add a set time out function like this and it does not work
setTimeout(myfunction(parameter),200)
meanwhile the code below work but it does not get the job done cause i cant pass a parameter.can anyone explain why ?
setTimeout(myfunction,200)
Try
setTimeout(function() {
myfunction(parameter);
}, 200)
You can also use
setTimeout(myfunction.bind(null, parameter), 200);

setInterval functionality in javascript using functions [duplicate]

This question already has answers here:
Pure Javascript - setInterval (1s), setAttribute
(2 answers)
Closed 5 years ago.
how can i implement the javascript code for setInterval with using only javascript functions(i should not be using setInterval(function(){},1000)). and i should not be using any kind of libraries for making that possible.
Can any one please help me with this???
You can use recursive setTimeout
var timerId = setTimeout(function run() {
alert( "sec" );
timerId = setTimeout(run, 1000);
}, 1000);

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

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)

Javascript setTimeout at onclick [duplicate]

This question already has answers here:
setTimeout ignores timeout? (Fires immediately) [duplicate]
(3 answers)
Closed 8 years ago.
I am trying to call a javascript function at the onclick of an html element as shown below.
But not sure why the below timeout function is not working here.Any idea
onclick="setTimeout(rating(), 3000)"
remove parenthesis, change:
onclick="setTimeout(rating(), 3000)"
to
onclick="setTimeout(rating, 3000)"
or better way would be:
onclick = setTimeout(function() {
rating();
}, 3000);
onclick="setTimeout(rating, 3000)" // rating without the ()
When you include the parentheses, the function return value is used instead of the function itself.

Categories