JS SetTimeout with SetInterval and for loop refresh UI

Source: Internet
Author: User

1. The SetTimeout () method is used to call a function or evaluate an expression after a specified number of milliseconds;

If you need to execute multiple times, you call SetTimeout () again;

Example: an infinite loop with a stop button

<HTML><Head><Scripttype= "Text/javascript">varC=0varTfunctionTimedcount () {document.getElementById ('txt'). Value=cc=C+1T=SetTimeout ("Timedcount ()", +)}functionStopcount () {cleartimeout (t)}</Script></Head><Body><form><inputtype= "button"value= "Start timing!" "OnClick= "Timedcount ()"><inputtype= "text"ID= "txt"><inputtype= "button"value= "Stop timing!" "OnClick= "Stopcount ()"></form><P>Please click on the "Start timing" button above. The input box will be timed from 0 onwards. Tap stop timing to stop the timing. </P></Body></HTML>

2. The SetInterval () method invokes a function or evaluates an expression according to the specified period (in milliseconds);

The SetInterval () method keeps calling functions until Clearinterval () is called or the window is closed. The ID value returned by SetInterval () can be used as a parameter to the Clearinterval () method.

Example:

<HTML><Body><inputtype= "text"ID= "Clock"size= "+" /><Scriptlanguage=javascript>var int=Self.setinterval ("clock ()", -)functionclock () {varT=NewDate () document.getElementById ("Clock"). Value=T}</Script><Buttononclick= "Int=window.clearinterval (int)">Stop interval</Button></Body></HTML>

Two: expansion

1. Use SetTimeout and SetInterval will not block the JS Web page of single-threaded page operation, such as using JS to change the page image or text on the page;

Typically using a For loop can affect the operation of the page;

        function Test ()        {            // If there are 4 images in the/tmp/directory, respectively Test0-3.png            // The following JS changes the ID of the testimg image;             for (var i=0;i<4;i++)            {                alert (i);                document.getElementById ("testimg". src= "/tmp/test" +string (i) + ". png";            }                         // run results, on the Web page, will pop up 4 times alert, but the picture will not switch, but to wait until the last time to display the last picture;        }

Therefore, the above problem to use setTimeout, setinterval operation;

2. Another point, JS for the same ID of the IMG tag, in two times when changing the picture, if the image of the same name, the picture is not the same, the picture will not change;

That is the first time JS change the picture for a.png;

The second a.png picture changed, but the name is still a.png, but when you change with JS, the picture on the page does not refresh.

Reference: http://www.w3school.com.cn/jsref/met_win_settimeout.asp

Http://www.w3school.com.cn/jsref/met_win_setinterval.asp

JS SetTimeout with SetInterval and for loop refresh UI

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.