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