Why does the setInterval callback execute only once?
You used a function call instead of a function reference as the first parameter of the setInterval. Do it like this:
function timer() {
console.log("timer!");
}
window.setInterval(timer, 1000);
Or shorter (but when the function gets bigger also less readable):
window.setInterval( function() {
console.log("timer!");
}, 1000)
setInterval fires only once
I modified your code a bit and changed the line containing setInterval
to this:
this.timerId = setInterval((function(scope) {
return function() {
scope.decrementCounter(scope);
};
})(this), 1000);
JS setInterval executes only once
You are passing the result of executing the function instead of the function itself. Since the result of the function is undefined, you are executing checkIfGameAlreadyStarted and then passing undefined to setInterval which doesn't do anything.
Instead of this:
setInterval(checkIfGameAlreadyStarted(), 1000);
Your statement should be this:
setInterval(checkIfGameAlreadyStarted, 1000);
without the parentheses at the end of the function name.
When you pass checkIfGameAlreadyStarted()
that calls the function immediately and gets it's return value. When you pass checkIfGameAlreadyStarted
that passes a reference to the function so setInterval can call it later (which is what you want).
Why my setInterval runs only once?
You are only executing the function initially and setting the returned value as an argument instead use a callback function.
<body onload="startTimer('timer')">
<h1 id="timer">
</h1>
</body>
<script type="text/javascript">
function printTime(arg) {
var mydate = new Date();
document.getElementById(arg).innerHTML = mydate;
}
function startTimer(arg) {
setInterval(function() {
printTime(arg)
}, 1000);
}
</script>
setInterval function works only once
You have to set the interval function correctly:
function autoUpdate() {
intervalId = setInterval(function(){
updateFile(fileId, 'root', document.getElementById('editor').value)
}, 10000);
}
What you are doing is actually calling the function once instead of passing it.
The clearInterval itself looks correct.
javascript setInterval only work once?
You need to wrap the calling part in a function, because it is called only once and returns undefined
as calling value.
setInterval(() => updateClock($('#clock')), 1000);
Another possibillity is to use the third and following arguments of setInterval
setInterval(updateClock, 1000, $('#clock'));
Javascript Function setInterval() works only one time
setInterval
expects a callback as first argument, but you are calling the actual function.
Call should be like below
setInterval(function() {
below(t++); }
,1);
So here you are creating an anonymous callback which will call your function below. And better put the exit condition t >= 50
inside below
function
Weirdly javascript setInterval executes only once in this case with this class
I get error if I pass test.method1, why ?
Because this
context is lost. If you console.log(this)
inside method1
, you will not get an instance of Test
. Basically, it's
const orphanedMethod = test.method1; // I'm not bound to the test variable now...
setInterval(orphanedMethod, 1000); // and if I try to access `this`, I'll get Window or sth like that
Either make method1
an arrow function:
method1 = () => {
let value;
value = this.method2();
console.log(this.counter);
}
or explicitly bind it to the object when calling:
setInterval(test.method1.bind(test), 1000);
setInterval in a promise, only executes once in React
You pass the returned value of createTimer
to setInterval
, instead pass the function but don't invoke it
setInterval(this.createTimer, 1000)
Related Topics
Indexof Method in an Object Array
How to Pass Parameters Rendered from Backend to Angular2 Bootstrap Method
Creating Object With Dynamic Keys
Console.Log() Shows the Changed Value of a Variable Before the Value Actually Changes
No Visible Cause For "Unexpected Token Illegal"
Group Array of Object Nesting Some of the Keys With Specific Names
How to Connect to SQL Server Database from JavaScript in the Browser
Can't Perform a React State Update on an Unmounted Component
How to Check Whether a Radio Button Is Selected With JavaScript
How to Invoke a Jsf Managed Bean on a HTML Dom Event Using Native JavaScript
Jquery in Greasemonkey 1.0 Conflicts with Websites Using Jquery
Simulate Background-Size:Cover on <Video> or <Img>