美文网首页
setInterval与setTimeout的区别

setInterval与setTimeout的区别

作者: 码课sir | 来源:发表于2018-08-12 14:55 被阅读0次

相同点

setInterval与setTimeout都可实现倒计时执行

区别

setInterval设置后每隔指定时间会无限执行
setTimeout从运行开始只在设置的时间执行一次

例题

setInterval

<script>
        function hello(){
            alert('hello');
        }
       // 有两种使用方法
      //方法一:setInterval(hello,3000);
      //放法二:setInterval('hello()',3000);
</script>

setTimeout

<script>
        function hello(){
            alert('hello');
        }
       // 也有两种使用方法
      //方法一:setTimeout(hello,3000);
      //放法二:setTimeout('hello()',3000);
</script>

总结

两种方法根据不同的场景和业务需求择而取之,

一般情况下setTimeout用于延迟执行某方法或功能,

setInterval则一般用于刷新表单,对于一些表单的假实时指定时间刷新同步

相关文章

网友评论

      本文标题:setInterval与setTimeout的区别

      本文链接:https://www.haomeiwen.com/subject/lpusbftx.html