Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call method form setInterval() causing exception

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 ?

like image 979
Jviaches Avatar asked Jun 29 '14 14:06

Jviaches


People also ask

How does the setInterval () function?

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

How do you call setInterval?

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.

How do you call a setInterval function in TypeScript?

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.

Why is it important to store the value returned by the call to setInterval ()?

setInterval returns an ID which you can later use to clearInterval(), that is to stop the scheduled action from being performed.


2 Answers

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);
like image 121
Anthony Chu Avatar answered Sep 17 '22 00:09

Anthony Chu


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!

like image 22
Carlos Galeano Avatar answered Sep 20 '22 00:09

Carlos Galeano