JQuery and JS implement the method of pausing loops, and jqueryjs pauses Loops

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.