JQuery and JS implement the method of pausing loops, and jqueryjs pauses Loops
This article describes how jQuery and JS pause in a loop. Share it with you for your reference. The specific analysis is as follows:
Problem:
Jquery performs an array loop. If it is required to suspend the array for 2 seconds during each loop, the array will not be paused no matter how it is set when jQuery's. earch loop is used.
SetTimeout is only paused during the first execution.
Cause prediction:
How does js start executing multithreading?
Improvement:
Change setInterval to control the loop. When the maximum value of the array is reached, clearInterval cancels the loop.
The following is the jQuery code:
<script type="text/javascript">var arr = new Array();var arrk = 0;jQuery(function() {arr[0] = "aaa.keleyi.com";arr[1] = "bbb.keleyi.com";arr[2] = "ccc.keleyi.com";mytime = setInterval(function(){showme()}, 2000);function showme(){alert(arr[arrk]);arrk += 1;if(arrk>2) clearInterval(mytime);}});</script>
The following is the Javascript code:
<script type="text/javascript">var arr = new Array();var arrk = 0; arr[0] = "aaa.keleyi.com";arr[1] = "bbb.keleyi.com";arr[2] = "ccc.keleyi.com";mytime = setInterval(function () { showme() }, 2000);function showme() {alert(arr[arrk]);arrk += 1;if (arrk > 2) clearInterval(mytime);}</script>
I hope this article will help you design javascript programs.