Synchronous execution of setimeout [duplicate] - javascript

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/

Related

JS setTimeout Not Delayed [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 3 years ago.
I'm trying to put a delay in front of an AJAX call.
var delay = 2000;
$("#c_name").on("keyup", function() {
var entered = $(this).val();
if (entered.length > 1) {
setTimeout(dosearch(entered), delay)
}
});
Fo some reason I can't seem to get setTimeout to take hold. It's performing the dosearch() function instantly.
How can I get this to delay properly? Yes JQuery 3.3.1 is loaded up top.
Answer: Both of these work:
setTimeout(() => dosearch(entered), delay)
setTimeout( function() { dosearch(entered) }, delay)

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 JS function doesn't work? [duplicate]

This question already has answers here:
Why is my function call that should be scheduled by setTimeout executed immediately? [duplicate]
(3 answers)
Closed 7 years ago.
I'm trying to call a function every x-seconds,
setInterval( alert("Hello"),2000);
But the alert function appear just for the first call,
You may try this:
setInterval(function() {
alert("Hello");
} ,2000);
It's not working like you thought. The alert fires/runs directly without interval because you've directly called it. To be more clerer, in your code:
setInterval(alert("Hello"),2000);
You are calling the alert("Hello") but you should pass a function (or bind) which you want to run on an interval, which is:
setInterval(function(){
// ...
}, 2000);
So the passed anonymous function will be called using the given interval (bind is another way but I've used a function/closure for simplicity to clarify you).

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