国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript學習筆記之定時器

2024-05-06 16:15:06
字體:
來源:轉載
供稿:網友
本文通過2個定時器的示例向我們展示了javascript中定時器的使用方法,格式以及功能,希望通過本文能夠讓大家對javascript定時器有新的認識。
 

定時器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>

 

以上就是本文的全部內容了,希望大家能夠喜歡


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 堆龙德庆县| 高雄县| 衢州市| 赫章县| 武宁县| 安新县| 涟源市| 清涧县| 屯留县| 崇礼县| 嘉祥县| 都兰县| 固阳县| 集贤县| 隆安县| 临澧县| 潞城市| 紫云| 平罗县| 无为县| 龙口市| 营口市| 威信县| 肇东市| 共和县| 长丰县| 扎鲁特旗| 中宁县| 车致| 确山县| 灵山县| 岑巩县| 英超| 白山市| 澄城县| 惠水县| 镇原县| 满洲里市| 郸城县| 郸城县| 成武县|