I would like to call function from setInterval(). Here is the idea:
class Greeter {
element: HTMLElement;
span: HTMLElement;
timerToken: number;
constructor(element: HTMLElement) {
this.element = element;
this.element.innerHTML += "The time is: ";
this.span = document.createElement('span');
this.element.appendChild(this.span);
this.span.innerText = new Date().toUTCString();
//this.element.style.cssText = "-webkit-transform:rotate(7deg)";
//this.element.style.transition = "-webkit-transform: rotate(180deg)";
}
start() {
this.timerToken = setInterval(this.runningLoop(this.element), 500);
}
stop() {
clearTimeout(this.timerToken);
}
runningLoop(element: HTMLElement) {
this.element.style.cssText = "-webkit-transform:rotate(7deg)";
}
}
window.onload = () => {
var el = document.getElementById('content');
var greeter = new Greeter(el);
greeter.start();
};
In this case i getting an exception:
Unhandled exception at line 13, column 9. Microsoft JScript runtime error: Invalid argument.
So i tried as following :
this.timerToken = setInterval(function () { this.runningLoop(this.element) }.bind, 500);
No exception but nothing happens..
Any ideas ?
setInterval() The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. This method returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval() .
The setInterval() method is JavaScript is used to evaluate an expression at intervals. Here's the syntax: setInterval(function, interval_in_milliseconds, param1, param2, param3...) Here, interval_in_milliseconds sets the intervals in milliseconds, after the code will execute.
Syntax of setInterval TypeScriptlet timer = setInterval(callback function, delay,[arg1, arg2, arg3, ...]); As per the syntax, function() is actually the callback function to be executed for each millisecond.
setInterval returns an ID which you can later use to clearInterval(), that is to stop the scheduled action from being performed.
setInterval(this.runningLoop(this.element), 500);
The above invokes this.runningLoop
before passing it to setInterval
, setInterval
is expecting a function but is receiving undefined
. Wrap the call in an arrow function...
setInterval(() => this.runningLoop(this.element), 500);
And since you're not using the element argument in runningLoop
, you can remove the argument and pass the method to setInterval
...
setInterval(this.runningLoop, 500);
This will be looping in the time that you want: setInterval(() => function(), time in miliseconds);
Now, if you want to stop whenever! Write a var before the interval: Declare on the top of your TS file: varInterval: any; this.varInterval = setInterval(() => function(), time in miliseconds);
You can call the method to stop like this: clearInterval(this.varInterval);
This work for me!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With