How to get the instance name of an object

后端 未结 7 1328
萌比男神i
萌比男神i 2021-01-15 00:07

I use the below code to write code to query a web method in a specified interval.

now in the this.Poll function I have to do

this.tmo = setTimeo         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-15 00:30

    You can use this handy little wrapper to execute a function periodically at given interval (1 sec by default):

    function tick(func, interval) {
        return (function() {
            if(func())
                setTimeout(arguments.callee, interval || 1000);
        })();
    }
    

    The function is repeated until it returns false:

    tick(function() {
       if(should stop) return false;
       do stuff
       return true;
    });
    

    If the function is a method, make a closure as shown

    // inside your object
    var me = this;
    tick(function() {
       return me.doStuff();
    });
    

提交回复
热议问题