zoukankan      html  css  js  c++  java
  • javascript 定时任务封装

    /** 
     * 定时任务 
     * 间隔时间,执行次数,要带的参数,要执行的函数. 
     */  
    var TimingTask = function(time,count,param,fun){  
        this.id = -1;           //编号  
        this.exectionCount = 0; //执行了多少次  
        if(typeof time === 'function'){  
            this.fun = time;  
            this.time = 3000;  
            this.count = -1;  
        }else  
        if(typeof param === 'function'){  
            this.time = time;  
            this.count = count;  
            this.fun = param;  
        }else  
        if(typeof count === 'function'){  
            this.time = time*1000;  
            this.fun = count;  
            this.count = -1;  
        }else{  
            this.time = time*1000;  
            this.count = count;  
            this.param = param;  
            this.fun = fun;  
        }  
    }  
    TimingTask.prototype = {  
        add : function(time,count,fun){ //间隔秒数,执行次数,执行方法  
            if(typeof time === 'function'){  
                this.fun = time;  
            }else  
            if(typeof count === 'function'){  
                this.time = time;  
                this.fun = count;             
            }else{  
                this.time = time;  
                this.count = count;  
                this.fun = fun;  
            }  
        },  
        start : function(){  
            if(this.id === -1){ //说明还没开始  
                this.exectionCount = 0;  
                this.id = setInterval((function(param) {  
                    return function() {  
                        if(param.count > 0){  
                            if(param.exectionCount >= param.count){  
                                param.stop();  
                                return;  
                            }  
                        }  
                        param.exectionCount ++;  
                        param.fun(param);  
                    }  
                })(this), this.time);  
            }  
        },  
        stop : function(){  
            clearInterval(this.id);  
            this.id = -1;  
        }  
    };  
      
    //使用方法  
    var chart = {test:'testString'};  
    var change3D = new TimingTask(0.4,1,chart,function(chartObj){   //0.4秒后改回3D效果,执行1次就够了.  
                         var chart = chartObj.param;  
                         alert(chart.test);              });  
                     change3D.start();  //执行  

    参考文献:

    [1].  JS定时任务封装 - - ITeye博客

     

  • 相关阅读:
    使用jaxb用xsd生成java类
    EMF保存CDATA
    windows builder里面的可伸缩面板
    使用eclipse open type对话框
    eclipse中toolbar位置的系统URI
    bzoj 4414 数量积 结论题
    bzoj 4402 Claris的剑 组合数学
    bzoj 4206 最大团 几何+lis
    bzoj 3676 [Apio2014]回文串 回文自动机
    bzoj 3670 [Noi2014]动物园 kmp
  • 原文地址:https://www.cnblogs.com/yqmcu/p/10537745.html
Copyright © 2011-2022 走看看