setInterval functionality in javascript using functions [duplicate] - javascript

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

Related

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 can I set delay for running functions [duplicate]

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

Synchronous execution of setimeout [duplicate]

This question already has answers here:
Sleep in JavaScript - delay between actions
(15 answers)
What is the JavaScript version of sleep()?
(91 answers)
Closed 11 months ago.
How would I convert the following:
while True:
# do something
time.sleep(2)
into JavaScript?
You would not, as JavaScript does not sleep - it is synchronous and event-based. Yet, you can schedule functions to be executed later in time via setTimeout and setInterval:
var timerid = setInterval(function() {
// do something
// instead of "break", you'd use "clearTimeout(timerid)"
}, 2000);
For your ajax progress bar, I'd recommend the following which does not fire requests strictly each 2s, but waits for them to return:
function getUpdate() {
myAjax(…, function onAjaxSuccess(result) { // an async event as well
// show(result)
if (!result.end)
setTimeout(getUpdate, 2000);
});
}
getUpdate();
setInterval(function() {
console.log('do something');
}, 2000);
http://jsfiddle.net/rS9bH/

NodeJS: Settimeout in prototypes [duplicate]

This question already has answers here:
setTimeout scope issue
(5 answers)
Closed 8 years ago.
I recently started using NodeJS, but when I do
I'm getting errors such as:
What's the best way to do this?
My current solution is like this:
var that = this;
setTimeout(function()
{
that.myMethod();
}, 3000);
you can use the old that=this trick, or use bind, since it's sure to work in node.js:
setTimeout(this.myOtherMethod.bind(this), 10);

Python while loop conversion to Javascript [duplicate]

This question already has answers here:
Sleep in JavaScript - delay between actions
(15 answers)
What is the JavaScript version of sleep()?
(91 answers)
Closed 11 months ago.
How would I convert the following:
while True:
# do something
time.sleep(2)
into JavaScript?
You would not, as JavaScript does not sleep - it is synchronous and event-based. Yet, you can schedule functions to be executed later in time via setTimeout and setInterval:
var timerid = setInterval(function() {
// do something
// instead of "break", you'd use "clearTimeout(timerid)"
}, 2000);
For your ajax progress bar, I'd recommend the following which does not fire requests strictly each 2s, but waits for them to return:
function getUpdate() {
myAjax(…, function onAjaxSuccess(result) { // an async event as well
// show(result)
if (!result.end)
setTimeout(getUpdate, 2000);
});
}
getUpdate();
setInterval(function() {
console.log('do something');
}, 2000);
http://jsfiddle.net/rS9bH/

Categories