2017-08-02 51 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); 
});