當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JavaScript学习05 定时器
生活随笔
收集整理的這篇文章主要介紹了
JavaScript学习05 定时器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
JavaScript學習05 定時器
?
定時器1
用以指定在一段特定的時間后執行某段程序。
setTimeout():
格式:[定時器對象名=] setTimeout(“<表達式>”,毫秒)
功能:執行<表達式>一次。
例子:
<!DOCTYPE html> <html><head><title>timer1.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!--<link rel="stylesheet" type="text/css" href="./styles.css">--><script type="text/javascript">function count(){setTimeout("alert('執行成功!')",7000);}</script></head><body><input type="button" value="點擊我啊" onclick="count();"></body> </html>?
?
定時器2
以一定的時間為間隔,不斷地重復執行表達式。
setInterval():
格式:[定時器對象名=] setInterval(“<表達式>”,毫秒)
功能:重復執行<表達式>,直至窗口、框架被關閉或執行clearInterval。
clearInterval():
格式:clearInterval(定時器對象名)
功能:終止定時器
例子:
<!DOCTYPE html> <html><head><title>timer2.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!--<link rel="stylesheet" type="text/css" href="./styles.css">--><script type="text/javascript">var sec = 0;var timer = setInterval("count();",1000);//頁面加載的時候即開始計時function count(){document.getElementById("num").innerHTML = sec++;}function stopCount(){clearInterval(timer);//停止定時器的運行 }</script></head><body><font color="red" id="num">0</font><input type="button" value="停止" onclick="stopCount();"></body> </html>?
參考資料
圣思園張龍老師Java Web視頻教程。
W3School JavaScript教程:http://www.w3school.com.cn/js/index.asp
英文版:http://www.w3schools.com/js/default.asp
?
JS Timing:http://www.w3school.com.cn/js/js_timing.asp
?
轉載于:https://www.cnblogs.com/mengdd/p/3680863.html
總結
以上是生活随笔為你收集整理的JavaScript学习05 定时器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 进程Process基本的操作说明
- 下一篇: 程序清单3-1 测试能否对标准输入设置偏