2017-08-02 52 views
-2

我创建一个滑块并使用setTimeout函数每5秒自动更改一次类名称。clearTimeout不支持单击事件

我想我每次点击幻灯片选择再次复位定时器但它并不无论我做什么了clearTimeout只是不从下面烧

function autoAddClass(){ 
     console.log('changing class'); 
     setTimeout(autoAddClass, 1000); 
    } 

    setTimeout(autoAddClass, 1000); // to run the function on load 


    $('.post-nav-btn').on('click',function(e){ 
     e.preventDefault(); 

     clearTimeout(autoAddClass); 
    }); 
again..pseudo代码停止功能
+0

您没有正确使用clearTimeout。通过https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearTimeout阅读它 – j08691

回答

2
var someVar = setTimeout(autoAddClass, 1000); 

//clear the timeout, you don't clear the function 
clearTimeout(someVar); 
0

您没有以正确的方式使用setTimeOut。 在您的代码中,您正在清除该功能。

请参阅此链接了解如何使用它。 W3School

我认为这是您想要的解决方案。

var timeout = null; 
function autoAddClass(){ 
     console.log('changing class'); 

} 
timeout = setTimeout(autoAddClass, 1000); // to run the function on load 

$('.post-nav-btn').on('click',function(e){ 
     e.preventDefault(); 

     clearTimeout(timeout); 
});