wuxiaoru

1、定时分为单次定时器跟多次定时器

(1)单次定时器

setTimeout   单次定时器;

clearTimeout  清除单次定时器

var obj=setTimeout(function(){},1000)
clearTimeout(obj);

(2)多次定时器

setInterval   多次定时器

clearInterval    清除多次定时器

var obj=setInterval(function(){},1000)
clearInterval(obj);

实例如下:

      <script>
                // 多次定时器
                var obj=setInterval(function(){
                    console.log(1);
                    clearInterval(obj);   //  在多次定时器执行一次后,便清除该多次定时器,clearInterval放在此处,多次定时器只执行一次
                },1000)
                // clearInterval(obj);     //清除多次定时器,多次定时器不会执行
        </script>

2、一个关于定时器的使用实例

当点击删除按钮时,会弹出“已删除”字样,几秒过后,“已删除”字样不再显示

    <body>
        <div id="status"></div>
        <input type="button" value="删除" onclick="DeleteEle();" />
        <script>
            function DeleteEle(){
                document.getElementById(\'status\').innerText="已删除";
                setTimeout(function(){
                    document.getElementById(\'status\').innerText="";
                },5000)
            }
        </script>
    </body>

执行结果如下:

当点击删除按钮时,弹出字样“已删除”:

5秒后,“已删除”字样不再显示。

 

 

 



 

分类:

技术点:

相关文章:

  • 2021-12-03
  • 2021-11-15
  • 2022-01-13
  • 2021-09-07
  • 2021-11-04
  • 2018-01-22
  • 2021-12-26
  • 2021-11-23
猜你喜欢
  • 2021-12-31
  • 2021-09-17
  • 2021-08-16
  • 2021-07-25
  • 2021-10-03
  • 2021-12-10
  • 2021-03-31
相关资源
相似解决方案