前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >setTimeout和setInterval

setTimeout和setInterval

作者头像
鹤川
发布2023-03-21 21:44:56
1K0
发布2023-03-21 21:44:56
举报
文章被收录于专栏:鹤川博客鹤川博客

setTimeout 和 setInterval

  • setTimeoutsetInterval,也是浏览器中的内置函数,属于 JavaScript 代码
  • setTimeout:表示多久之后执行
    • 语法 setTimeout(func, time), time 是毫秒
    • 可以通过 clearTimeout 函数对 setTimeout 进行取消
  • setInterval:间隔多长时间循环执行
    • 语法 setInterval(func, time), time 是毫秒
    • 可以通过 clearInterval 函数对 setInterval 进行取消

一、代码实战

新建 html 文件 21-setTimeout.html ,编写下方程序,运行看看效果吧

代码语言:javascript
复制
<!DOCTYPE html>
<htmllang="en">
  
    <head>
        <metacharset="UTF-8">
        <metahttp-equiv="X-UA-Compatible"content="IE=edge">
        <metaname="viewport"content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
  
    <body>
        <buttononclick="cancelExe()">取消执行</button>

        <scripttype="text/javascript">
            function outFunc(){
                alert("setTimeout")
            }
            let to = setTimeout(outFunc,3000)//3秒

            function inFunc(){
                alert("setInterval")
            }
            let ti = setInterval(inFunc,3000)

            function cancelExe(){
                clearTimeout(to)
                clearInterval(ti)
            }
        </script>
    </body>
  
</html>
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023 年 02 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • setTimeout 和 setInterval
    • 一、代码实战
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
    http://www.vxiaotou.com