简体   繁体   English

如何使用对象方法作为参数使Javascript对象的方法调用setInterval

[英]How to you make a Javascript object's method call setInterval with an object method as an argument

I am trying to create a generic countdown timer object in Javascript I have a method (decrementTimer) that reduces the timeLeft on counter by a fixed amount and another method which uses setInterval to call decrementTimer method. 我试图在Javascript中创建一个通用的倒数计时器对象,我有一个方法(decrementTimer)可以将timeLeft减少固定的数量,另一个方法是使用setInterval调用decrementTimer方法。

The problem is that the code only runs once not every second. 问题在于代码仅每秒运行一次。 It looks like setInterval isn't putting the decrementTimer function on the queue inside my object. 看起来setInterval并未将decrementTimer函数放在对象内部的队列中。

I have tried making javascript do an Eval by putting the keyword "window" in front of the setIntervalfunction call but this doesn't work. 我试图通过将关键字“ window”放在setIntervalfunction调用前面来使javascript进行Eval,但这不起作用。

I can't use a Javascript class because I can't assume that all browsers support ECMAScript 6. I am using IE11. 我不能使用Javascript类,因为我不能假定所有浏览器都支持ECMAScript6。我正在使用IE11。

I have also found solutions that work when you are doing this in a function but no examples of how to make this work in an object. 我还发现了在函数中执行此操作时可以使用的解决方案,但没有如何在对象中执行此操作的示例。

I would appreciate some help. 我将不胜感激。

<script>
var myTimer = {
    timeLeft:       10,
    timerJobNumber: null,  
    decrementTimer: function(){
    alert("decrementTimer called. timeLeft = " + this.timeLeft);
    this.timeLeft = this.timeLeft - 1;
        if(this.timeLeft<0){
            alert("done");
        }
    },
    startTimer: function(){
    alert("startTimer called");
    this.timerJobNumber = window.setInterval(this.decrementTimer(),10);
    alert("Interval Job Number = " + this.timerJobNumber);
               },

    stopTimer: function(){
    clearInterval(this.timerJobNumber);
    alert(this.timerJobNumber);
    alert("job terminated");
    },

    resetTimer: function(initialTime){
    this.TimeLeft = initialTime;
    alert("intitialTime="+intitialTime);
    },
    getTimeLeft: function(){
        return this.timeLeft;
    }  
};

console.log(myTimer.getTimeLeft());
console.log(myTimer.startTimer() );
console.log(myTimer.getTimeLeft());

</script> 

I didn't really check all of your code, but this seems to do what you want : 我并没有真正检查所有代码,但这似乎可以满足您的要求:

var myTimer = {
    timeLeft:       10,
    timerJobNumber: null,  
    decrementTimer: function(){
        console.log("decrementTimer called. timeLeft = " + this.timeLeft);
        this.timeLeft = this.timeLeft - 1;
        if(this.timeLeft<0){
            this.stopTimer();
        }
    },
    startTimer: function(){
        this.timerJobNumber = window.setInterval(this.decrementTimer.bind(this),1000);
        console.log("Interval Job Number = " + this.timerJobNumber);
    },

    stopTimer: function(){
        clearInterval(this.timerJobNumber);
        alert(this.timerJobNumber);
    },

    resetTimer: function(initialTime){
        this.TimeLeft = initialTime;
        alert("intitialTime="+intitialTime);
    },
    getTimeLeft: function(){
        return this.timeLeft;
    }  
};

Note that you can easily transform this into a class like function : 请注意,您可以轻松地将其转换为类似 function的

var MyTimer = function() {
    this.timeLeft = 10;
    this.timerJobNumber = null;
};

MyTimer.prototype.decrementTimer = function() {
    console.log("decrementTimer called. timeLeft = " + this.timeLeft);
    this.timeLeft = this.timeLeft - 1;
    if(!this.timeLeft > 0)
        this.stopTimer();
};

MyTimer.prototype.startTimer = function() {
    this.timerJobNumber = window.setInterval(this.decrementTimer.bind(this),1000);
    console.log("Interval Job Number = " + this.timerJobNumber);
};

MyTimer.prototype.stopTimer = function() {
    clearInterval(this.timerJobNumber);
    alert(this.timerJobNumber);
};

MyTimer.prototype.resetTimer = function(initialTime) {
    this.timeLeft = initialTime;
    alert("intitialTime="+intitialTime);
};

MyTimer.prototype.getTimeLeft = function() {
    return this.timeLeft;
};

//...

var m = new MyTimer();
m.startTimer();

I think you don't bind your decrementTimer() inside the setInterval() function. 我认为您不会在setInterval()函数内绑定您的decrementTimer()

startTimer: function(){
    alert("startTimer called");
    this.timerJobNumber = window.setInterval(this.decrementTimer.bind(this),10);
    alert("Interval Job Number = " + this.timerJobNumber);
}

if you are not familiar this topic, then follow this link. 如果您不熟悉此主题,请单击此链接。 I think it will help you. 我认为它将为您提供帮助。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM