jQuery不同函数中的startInterval和clearInterval

jQuery startInterval and clearInterval in different functions

本文关键字:startInterval clearInterval 函数 jQuery      更新时间:2023-09-26

我正在尝试构建一个有点面向对象的jQuery插件。一切都很顺利,但我似乎无法正确地实现开始/暂停功能。我有以下两个函数:

this.startAutoPlay = function() {
    var interval = setInterval(function() { 
        obj.gotoNext(); 
    }, config.timing);
};
this.stopAutoPlay = function() {
    clearInterval(obj.startAutoPlay);
};

我只需要从stopAutoPlay函数内访问间隔变量的方法。

指针吗?

您需要清空您设置的间隔。

this.interval;
this.startAutoPlay = function() {
    obj.interval = setInterval(function() { 
        obj.gotoNext(); 
    }, config.timing);
};
this.stopAutoPlay = function() {
    clearInterval(obj.interval);
};