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)
Related
This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 10 months ago.
Why does declaring var timer = setTimeout(etc.) automatically execute the setTimeout() function? And how would one pass a setTimeout() function as a variable without having the setTimeout() automatically execute?
var do_this = function(params){console.log("executes automatically, but function is only declared")};
var delay = 50;
var timeoutID = setTimeout(do_this(), delay) //executes automatically
Functions such as setTimeout require a callback-function that can be called the moment the timer expires. When you just provide a function, it is called immediately because it is never added to the callback-queue.
Thus, you should pass it a callback-function:
setTimeout( () => ... )
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);
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);
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/
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/