您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JavaScript中setTimeout()的使用详解

2024/10/28 9:10:13发布48次查看
javascript中settimeout方法用处比较多,通常用在页面刷新了、延迟执行了等等、但是很多javascript新手对settimeout的用法还是不是很了解、今天就给大家介绍下javascript中settimeout()的使用详解!
settimeout的用法
让我们一起来运行一个案例,首先打开记事本,将下面代码贴入,运行一下效果!
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> </head> <body> <h1> <font color=blue> haorooms博客示范网页 </font> </h1> <p> 请等三秒!</p> <script> settimeout("alert('对不起, haorooms博客要你等候多时')", 3000 ) </script> </body> </html>
页面会在停留三秒之后弹出对画框!这个案例应用了settimeout最基本的语法,settimeout不会自动重复执行!
settimeout也可以执行function,还可以不断重复执行!我们再来一起做一个案例:
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <script> var x = 0 function countsecond() { x = x+1   document.haorooms.haoroomsinput.value=x   settimeout("countsecond()", 1000) } </script> </head> <html> <body> <form name="haorooms"> <input type="text" name="haoroomsinput"value="0" size=4 > </form> <script> countsecond() </script> </body> </html>
你可以看到input文本框中的数字在一秒一秒的递增!所以,settimeout也可以制作网页中的时间跳动!
没有案例,学习起来不会很快,我们再来一起做一个例子,计算你在haorooms某个页面的停留时间:
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <script> x=0 y=-1 function countmin() { y=y+1   document.displaymin.displaybox.value=y   settimeout("countmin()",60000) } function countsec() { x = x + 1   z =x % 60   document.displaysec.displaybox.value=z   settimeout("countsec()", 1000) } </script> </head> <body> <table> <tr valign=top> <td> 你在haorooms博客中的停留时间是: </td> <td> <form name=displaymin> <input type=text name=displaybox value=0 size=4 > </form> </td> <td> 分 </td> <td> <form name=displaysec> </td> <td> <input type=text name=displaybox value=0 size=4 > </form> </td> <td> 秒。</td> </tr> </table> <script> countmin() countsec() </script> </body> </html>
怎么样,通过上面的例子,对settimeout()的用法,相信你都了解了吧!
总结:
通过上面的讲解,不知道您对settimeout了解了没有,下次使用settimeout会不会很熟练、希望对你的工作有所帮助!
相关推荐:
settimeout实例
js中的settimeout()函数
javascript函数settimeout带参数用法实例详解
从settimeout来谈谈说事件循环模型
以上就是javascript中settimeout()的使用详解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product