Two methods to achieve JS page after a few seconds to jump, and the difference

Source: Internet
Author: User

The two object methods of window are needed here, setinterval () and settimeout ()

A. Difference:

1. SetInterval (code,millisec) periodically (millisec unit milliseconds) invokes or executes the code function or string

Define and use the SetInterval () method to invoke a function or evaluate an expression by a 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.
The syntax setinterval (code,millisec[, "Lang"]) parameter describes code required. The function to invoke or the code string to execute. Millisec must. The time interval, in milliseconds, between periodically executing or calling code.

2. SetTimeout (code,millisec) waits for millisec (in milliseconds) to execute the code function or string only one time

The syntax settimeout (code,millisec) parameter     describes code     required. The JavaScript code string to execute after the function to invoke. Millisec     required. The number of milliseconds to wait before executing the code.

Two. js page after a few seconds to jump

1. SetInterval

       <p id= "Page_div" > will automatically jump to the first page after 5 seconds </p><script language= "javascript" >var num = 4; Number of seconds to countdown var URL = "index.html"; var id = window.setinterval (' doupdate () ', 1000); function DoUpdate () {document.getElementById (' Page_div '). InnerHTML = ' will automatically jump to the first page after ' +num+ ' seconds, if (num = = 0) { Window.clearinterval (ID); window.location = URL; }num--;} </script>

2. SetTimeout

       <p id= "Page_div" > will automatically jump to the first page after 5 seconds </p><script language= "javascript" >var num = 4; Number of seconds to countdown var URL = "index.html"; Window.settimeout ("DoUpdate ()", "n"), function doupdate () {if (num! = 0) { document.getElementById (' Page_div '). InnerHTML = ' will automatically jump to the home page ' in ' +num+ ' seconds, num--;window.settimeout ("doupdate ()", 1000);} Else{num = 4;window.location = URL;}} </script>

Two ways to implement JS page after a few seconds to jump, and the difference

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.