1. The setTimeout() method is used to call a function or calculate an expression after the specified number of milliseconds.
setTimeout() executes code only once. If you want to call multiple times, use setInterval() or have the code itself call setTimeout() again.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>setTimeout</title> </head> <body> <div id='div1'> </div> </body> </html> <script type="text/javascript"> //Set countdown seconds var t = 10; //Show countdown seconds function showTime(){ t -= 1; document.getElementById('div1').innerHTML= t; if(t==0){ location.href='http://www.baidu.com'; } //Execute once every second, showTime() setTimeout("showTime()",1000); } //Execute showTime() showTime(); </script>2. The setInterval() method can call functions or calculate expressions according to the specified period (in milliseconds).
The setInterval() method will call the function continuously 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.
<html><body><input type="text" id="clock" size="35" /><script language=javascript>var int=self.setInterval("clock()",50)function clock(){var t=new Date()document.getElementById("clock").value=t}</script></form><button onclick="int=window.clearInterval(int)">Stop interval</button></body></html>Let me introduce the relevant reading below
1. For the setInterval() function, you can refer to the detailed explanation of the usage of the setInterval() function.
2. location.href can refer to the href property of the Location object.
3. For the innerHTML attribute, you can refer to the chapter on the usage of js' innerHTML attribute.
The following is an example to introduce it to you:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> Method for jumping pages in a timely manner</title> </head> <body> <script type="text/javascript"> var t=10;//Set the jump time setInterval("refer()",1000); //Start the 1-second timing function refer(){ if(t==0){ location="www.baidu.com"; //#Set the jump link address} document.getElementById('show').innerHTML=""+t+"Jump in seconds"; // Show countdown t--; // Counter decrement} </script> <span id="show"></span> </body> </html>Problems encountered:
When the above js method is placed in $(function(){......}), the browser will report methodXX() is not defined;
The definition of function(){} should be placed in <script></script>
The above is the JavaScript implementation countdown jump (recommended) introduced by the editor to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!