简体   繁体   English

在点击时更改setTimeout的持续时间

[英]Change duration of setTimeout on click

I'm working on this simple loop that display 1-20 numbers. 我正在研究一个显示1-20个数字的简单循环。 It was displayed one by one with a duration of 500ms apart. 它以500ms的间隔一一显示。 what I'm trying to do is, when the loop is on 10 then I click the button it will show instantly the remaining 10 numbers. 我想做的是,当循环在10上时,我单击按钮,它将立即显示剩余的10个数字。

Thanks. 谢谢。

 var duration = 500; for(let i=0; i < 20; i++ ){ setTimeout(function(){ $('ul').append('<li>'+ (i+1) +'</li>') }, i * duration); } $('button').click(function(){ duration = 0; }); 
 ul li{ list-style-type: none; float:left; width: 20px; color: #FFF; text-align: center; } ul li:nth-child(even){ background-color: #222; } ul li:nth-child(odd){ background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul></ul> <br> <button>display all</button> 

You could take a slightly different approach to the button click event, by emptying and rebuilding your <ul> list in the event that the button is clicked. 您可以对按钮单击事件采取稍微不同的方法,方法是在单击按钮的情况下清空并重建<ul>列表。

Something you also need to keep in mind is that your timeouts need to be cancelled when the button is clicked. 您还需要记住的一点是,单击按钮时需要取消超时。 To achieve this, you might consider storing each timeout's id, so that each timeout can be cleared when the button is clicked. 为此,您可以考虑存储每个超时的ID,以便在单击按钮时可以清除每个超时。

The following code is one approach to this, that minimises the amount of adjustment to your current code : 以下代码是实现此目的的一种方法,它可以最大程度地减少对当前代码的调整量:

  var duration = 500; // Declare timeout array to store timeout ids var timeouts = []; for(let i=0; i < 20; i++ ){ // Store each timeout id, so that you can cancel them if // button clicked timeouts[i] = setTimeout(function(){ $('ul').append('<li>'+ (i+1) +'</li>') }, i * duration); } $('button').click(function(){ // If button clicked, empty ul and repopulate it $('ul').empty() for(var i = 0; i < timeouts.length; i++) { // Clear timeout at i clearTimeout(timeouts[i]) // Add li element for i $('ul').append('<li>'+ (i+1) +'</li>') } duration = 0; }); 
 ul li{ list-style-type: none; float:left; width: 20px; color: #FFF; text-align: center; } ul li:nth-child(even){ background-color: #222; } ul li:nth-child(odd){ background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul></ul> <br> <button>display all</button> 

One option would be to have an array of functions that are shift ed and called on each interval. 一种选择是拥有在每个间隔上shift和调用的函数数组。 On click, clear the interval and forEach over the rest of the functions still in the array, and call them: 单击时,清除间隔和forEach仍在数组中的其余函数,然后调用它们:

 var duration = 500; const fns = []; for(let i=0; i < 20; i++ ){ const fn = () => $('ul').append('<li>'+ (i+1) +'</li>'); fns.push(fn); } const interval = setInterval(() => { const fn = fns.shift(); if (fn) fn(); else clearTimeout(interval); }, duration); $('button').click(function(){ fns.forEach(fn => fn()); clearTimeout(interval) }); 
 ul li{ list-style-type: none; float:left; width: 20px; color: #FFF; text-align: center; } ul li:nth-child(even){ background-color: #222; } ul li:nth-child(odd){ background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul></ul> <br> <button>display all</button> 

You can replace loop with function instead, rest works fine: 您可以将loop替换为function,其余可以正常工作:

 var duration = 500; function range(i, n) { setTimeout(function() { $('ul').append('<li>' + (i) + '</li>') if (i < n) { range(i + 1, n); } }, duration); } range(1, 20); $('button').click(function() { duration = 0; }); 
 ul li { list-style-type: none; float: left; width: 20px; color: #FFF; text-align: center; } ul li:nth-child(even) { background-color: #222; } ul li:nth-child(odd) { background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul></ul> <br> <button>display all</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM