Calling a class prototype method by a setInterval event

前端 未结 3 1102
甜味超标
甜味超标 2020-12-14 00:57

I have a simple javascript class.

One method of this class sets up a timer using setInterval function. The method that I want to call every time the event fires is

3条回答
  •  别那么骄傲
    2020-12-14 01:43

    setInterval can take a function directly, not just a string. https://developer.mozilla.org/en/DOM/window.setInterval

    i.e.

    loadingTimer = setInterval(showLoading, 100);
    

    But, for optimal browser compatibility, you should use a closure with an explicit reference:

     var t = this;
     loadingTimer = setInterval(function(){t.showLoading();}, 100);
    

提交回复
热议问题