confusion over setTimeout and clearTimeout

前端 未结 3 1344
春和景丽
春和景丽 2020-12-22 05:09

I have a button that changes the background of a div when its rolled over. the background needs to change on a timer so i have used setTimout to execute methods that change

3条回答
  •  滥情空心
    2020-12-22 06:00

    You are using setTimeout and clearTimeout wrongly. The setTimeout function returns a handle to the timeout, which is to be passed into clearTimeout to stop it.

    var playVTimeout;
    
    ...
    
        playVTimeout = setTimeout(playV, 2700);
    
    ....
    
        clearTImeout(playVTimeout);
    

    Also note that setTimeout(playV(), 2700); will call playV() now and execute its return value 2.7 seconds later. You should pass a function object playV in instead.

提交回复
热议问题