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>
  • 相关阅读:
    一个极好的ALV例子
    ABAP中读取文本文件(TXT DOCUMENT)并用ALV显示
    SMARTFORM报表程序设计(3)
    FOR ALL ENTRIES IN
    SMARTFORM报表程序设计(2)
    SMARTFORM报表程序设计(1)
    ABAP用FILE_OPEN_DIALOG打开多个文件后打印出名称
    LaTeX 表格的处理 [转]
    ubuntu安装deb,rpm安装包,tar.gz的方法 [转]
    OpenGL学习入门之VS2010环境配置 [转]
  • 原文地址:https://www.cnblogs.com/jiechn/p/4042567.html
Copyright © 2011-2022 走看看