In js, if I use a button to trigger a timer, it will be faster and faster to click on the timer many times. Why?

In

js, if I use a button to trigger a timer, it will be faster and faster to click on the timer many times. Why?

Dec.23,2021

it looks faster and faster because it starts one timer at a time


is not getting faster and faster, but more and more ;

$('button:first').click(function(){
    // ID
    var timerId = setInterval(function(){
        console.log('Do something...');
    }, /*3s*/3000);
    
    // , timerId
    // 
    // 
    clearInterval($(this).attr('data-timer-id'));
    $(this).attr('data-timer-id', timerId);
});

clear the timer first after clicking once. Before you have passed the last second, you will start next time. We should prevent shaking and throttle


timers are asynchronous and will not be executed in the order you expect, so you need to create a class to manage the order of timers. Refer to this article I wrote:
https://codeshelper.com/a/11.


then your code must not unbind the corresponding function after the button click event triggers the timer, causing you to trigger one more timer each time you click, and then give you the impression that it is getting faster and faster. In fact, the number of timers is increasing, this is your own code logic is not rigorous, yes, it belongs to bug!


your code is not rigorous. You should stop it first, regardless of whether it is opened or not, and then perform the opening

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb2239-23ab.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb2239-23ab.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?