Why does setInterval call my function once? [duplicate] - javascript

This question already has answers here:
JS setInterval executes only once
(2 answers)
Closed 4 years ago.
function start (argument){
alert("Starting Quiz!");
var time = setInterval( timer(), 1000);
}
function timer(){
console.log("Time: " + counter + " seconds");
}
I found that when setInterval( "timer()", 1000); works as intended repeatably calling my timer function, but when I don't use quotes "" the function is only called once. Why is that?

You're executing the function yourself. You should just pass the function:
var time = setInterval(timer, 1000);
When you passed a string, you were telling it to evaluate an expression in the global context.

Related

Why does declaring var timer = setTimeout(etc.) automatically execute the function? [duplicate]

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( () => ... )

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)

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/

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