调用原型中列出的函数

Era*_*arp -1 javascript

CircularCountDown.prototype = {
    init: function () {
        this.formatData();
        this.draw();
        this.start();
    },
    start: function () {
        if (typeof this.data.beforeStart == "function") {
            this.data.beforeStart(this);
        }
        this.show();
        this.starDecrementTimeEvent();
        var time = this.getFormattedTimeByCircle();
        this.animate(time);
    },
    starDecrementTimeEvent: function () {
        var that = this;
        console.log(that);
        this.decrementTimeEvent = setInterval(function () {
            that.time -= 1;
            that.setTime(that.getStringTime(that.time));
            if (that.time <= 0) {
                that.time = 0;
                that.stopDecrementTimeEvent();

                if (typeof that.data.end == "function") {
                    that.data.end(that);
                }
            }
        }, 1000);
    },
    stopDecrementTimeEvent: function () {
        clearInterval(this.decrementTimeEvent);
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用插件进行循环计时器,我希望调用stopDecrementTimeEvent函数来停止不同JS文件中的计时器.我不熟悉如何以这种格式调用它.

任何指导都会很棒,谢谢.

T.J*_*der 5

我正在使用插件进行循环计时器,我希望调用stopDecrementTimeEvent函数来停止不同JS文件中的计时器.我不熟悉如何以这种格式调用它

您将拥有一个您通过创建的实例new CircularCountDown,例如:

var theInstance = new CircularCountDown(/*...args here if needed...*/);
Run Code Online (Sandbox Code Playgroud)

创建的实例(对象)将具有CircularCountDown.prototype引用其原型的对象,这意味着它继承了其所有属性.

所以你只需要调用stopDecrementTimeEvent该实例:

theInstance.stopDecrementTimeEvent();
Run Code Online (Sandbox Code Playgroud)

这将stopDecrementTimeEvent在原型上找到属性,然后通过this引用实例(对象)来调用它,该实例将在其decrementTimeEvent属性中具有计时器句柄,准备使用它stopDecrementTimeEvent.