JS for loop timer settimeout invalid?

Source: Internet
Author: User

Recently doing the project in writing JS encountered such a problem: I need to write a similar to the matrix of the effect of the page will continue to have data to show 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M00/48/79/ Wkiol1qijunw4ox-aafuw44p4pm345.jpg "title=" qq picture 2222222222222222222.jpg "alt=" Wkiol1qijunw4ox-aafuw44p4pm345.jpg " />


So I thought about the timer function timing (500 milliseconds) on the page row of the data display but I also need to take other data in the background (not a one-time check out) so I want to use the outer nesting of a for loop so that JS will first take out the data timed display and then fetch the second paragraph of data results found JS does not support this The function of using the timer under the type for loop

for (Var i=1;i<10;i++) {setTimeout ("alert (' i= ' +i+" ') ", 100); Alert (i)}

This JS will not be executed in sequence, but will be processed below the following alert () in JS will be 100 milliseconds before the first to give you a for first execution end and then execute settimeout And because the threading problem JavaScript is unable to implement the PHP sleep function (pause a few seconds after the execution) many people have tried to write a JS sleep function but did not have to do so someone came up with some non-conventional way to get the card:

Method One: Set a function to allow the browser to do a hard calculation tens of thousands of times hundreds of thousands of times the browser suspended animation looks just like a pause.

Method Two: Perform timed place read time and then time operation execution time minus read time if exactly equal to delay time to execute

Both methods have a big problem: when the data is very small when the method two can be achieved but when the data is relatively large when the method of the two will cause the browser to die directly


As to how to deal with this problem or need to carefully study the data and process as far as possible in a timer to complete it! There are more ways than questions!





This article is from the "Jiawu notes" blog, so be sure to keep this source http://jiawu.blog.51cto.com/9349234/1548959

JS for loop timer settimeout invalid?

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.