setInterval Spotify apps
-
26-10-2019 - |
Question
I'm having trouble using setInterval/clearInterval.
I've tried to do something like this: int = setInterval(someFunction(), 1000);
but it only call someFunction()
once, instead of once every second?
So I tried to do this: int = setInterval("someFunction()", 1000);
and it actually works in some way, because it gives me this error Uncaught ReferenceError: someFunction is not defined
every second?
Why? D:
Solution
setInterval
takes two arguments: a function, and a time in milliseconds for the time between calls.
Your first example is wrong because it doesn't give a function as argument, it executes the function and passes the result as the first argument. Just change it to setInterval(someFunction, 1000)
and it'll work.
OTHER TIPS
This works fine for me:
this.interval = setInterval(function() {
console.log("tick");
}.bind(this), 100);