zoukankan      html  css  js  c++  java
  • window.clearInterval与window.setInterval的用法 定时器的设置

    window.setInterval()

    功能:按照指定的周期(以毫秒计)来调用函数或计算表达式。

    语法:setInterval(code,millisec) 

        code:在指定时间到时要执行的JavaScript代码串或函数。

        millisec:设定的定时时间,用毫秒数表示。

    返回值:定时器的ID值,可用于clearInterval()方法停止指定的定时器。

    注:setInterval()方法会不停地调用函数,直到用clearInterval()终止定时或窗口被关闭。

    window.clearInterval()

    功能:取消由setInterval()方法设置的定时器。

    语法:clearInterval(id_of_setinterval)

    解释:id_of_setinterval:由setInterval()返回的ID值。该值标识了一个setInterval定时器。

    也就是:window.setInterval()返回的就是window.clearInterval的参数

    例如:

      

    <div class="cg_content">
        <div class="success">
            <img src="images/success.png">
            <span>恭喜您 注册成功!</span>
        </div>
        <p>页面将在 <b>5</b> 秒内自动跳转到<a href="javascript:void(0)" onclick="tiger();"> 微信首页</a></p>
    </div>
    <div class="zc_footer"></div>
    <script type="text/javascript" src="jquery-1.11.1.min.js"></script>
    
    
    <script type="text/javascript">
        $(document).ready(function(){
            var i = 5;  设置为5秒
            var f = function(){
                i--;
                $(".cg_content p b").text(i);
                if(i == 0){
                    clearInterval(screen_time); 当等于0时,计时结束,进行跳转
                    window.location='http://www.baidu.com';
                }
                alert(screen_time);
            }
            var screen_time = setInterval(f,1000); 计时执行,一秒执行一次函数
    
        });
    
    
    </script>
    <script type="text/javascript">
        function tiger(){
            window.location='http://www.sina.com';
        }
    
    </script>
  • 相关阅读:
    根据用户输入的时间查询那天的数据
    动软 生成 linq相关DAO
    pdf 移除密码 去除水印 批量去除水印 编辑文字 批量替换文字
    利用OCR识别扫描的jpg、tif文件的文字
    jstat命令详解
    IDEA自动编译设置
    IntelliJ IDEA:Field injection is not recommended
    阿里巴巴Druid数据库连接池配置详解及使用
    com.mysql.jdbc.Driver和com.mysql.cj.jdbc.Driver的区别
    Java对元与分的金额的转换
  • 原文地址:https://www.cnblogs.com/jiechn/p/4042567.html
Copyright © 2011-2022 走看看