Search code examples
typescriptpolling

Exit polling with timeout


I am looking for an elegant way to break out of this poll:

private pollLinkResult() {
        (function poll() {
            setTimeout(() => {
                   //call the service layer to do an ajax call
                //depending on the result I would like to exit the infinite poll
            }, 1000);
        })();      
}

Any ideas?


Solution

  • private pollLinkResult() {
            (function poll() {
                let myTimeout = setTimeout(() => {
                       //call the service layer to do an ajax call
                    //depending on the result I would like to exit the infinite poll
    if(yourCondition){
    clearTimeout(myTimeout);
    }
                }, 1000);
            })();      
    }