javascript - Four countdowns, the function is triggered when it reaches 0. If penalties are imposed at the same time, how can the trigger event be executed only once?
大家讲道理
大家讲道理 2017-06-12 09:23:23
0
3
699

Penalty function when the countdown is 0. In one case, when all four countdowns are 0, the ajax() method will be triggered at the same time. In this case, I only want to trigger it once. What should I do?
I have tried to declare a count to count, and the ajax() method will not be executed if it exceeds 1. However, it is possible that the subsequent countdown will not be executed because it does not satisfy the logic that the count does not exceed 1. What should I do?

html 4:00//倒计时是循环的,到0后恢复为4分钟,其他一样 3:00 2:00 1:00 js $('.clock').each(function(elem,index){ $(this).on('finish',function(){//倒计时为0时触发ajax()方法 ajax(); }); });
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all (3)
大家讲道理

JS is single-threaded so it will not be triggered at the same time. You can maintain a variable to record the time when the function was last executed. If the interval is too close, it will not be executed a second time.

    伊谢尔伦

    Define a variable as the request status. The default is false, which means there is currently no request. Before the request is started, check whether it is true. If it is, it means that the last request is not continuing. Otherwise, set it to true and start executing the request. Ajax's complete method Set to restore the status to false regardless of success or failure so that it will be available for the next request. That is, use a global or local variable to control state.

      漂亮男人

      Try this:

      let loading = false $('.clock').each(function () { $(this).on('finish', function () { if (loading) return loading = true ajax({ finish: function () { loading = false } }) }) })
        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!