zoukankan      html  css  js  c++  java
  • js 倒计时

    //倒计时,分为时间格式和数字格式倒计时。带有具体格式
    /*使用示例
    $("#countDown").on("click",function(){
    MY.countDown.getTime({
    seconds:5,//倒计时数字
    flag:"#countDown",//页面标识
    type:"num",//倒计时类型
    action:function(){//倒计时为0时可以执行的方法
    $("#countDown").html("执行完毕");
    }
    });
    });
    */
    /*
    *para
    * 倒计时的秒数:obj.seconds
    * 返回数据类型:obj.type (time:时间类型,num:数字类型)
    * 插入文档中的位置:obj.flag
    * */
    ;(function(){
    var __countDown = {
    getTime:function(obj){
    var lt = [],day1,hour,minute,second;
    var seconds = parseInt(obj.seconds);
    var flag = obj.flag;
    var type = obj.type;
    if(type === "num"){//以数字形式展现出来的倒计时。
    if(!$(flag).data("go") && $(flag).data("go") != "go"){
    $(flag).data("go","go");
    }else{
    console.log("含有'data-go'属性,倒计时已经在执行");
    return false;
    }
    $(flag).text(__countDown.formatTimeNum(seconds));
    var countsId = setInterval(function(){
    seconds--;
    seconds = __countDown.formatTimeNum(seconds);
    if(seconds == "00"){
    if(obj && obj.action){
    obj.action();
    }else{
    $(flag).text("00");
    }
    $(flag).removeAttr("data-go");
    clearInterval(countsId);
    }else{
    $(flag).text(seconds);
    }
    },1000);
    }else{//以时间形式展现的倒计时
    if(!$(flag).data("go") && $(flag).data("go") != "go"){
    $(flag).data("go","go");
    }else{
    console.log("含有'data-go=go'属性,倒计时已经在执行");
    return false;
    }
    var leftTime = __countDown.leftTimeToArr(seconds);
    $(flag).html("<span>" + leftTime["day"] + "天&nbsp;</span>"+"<span>" + leftTime["hour"] + ":</span><span>" + leftTime["minute"] +":</span><span>" + leftTime["second"] + "</span>");
    var intervalId = setInterval(function(){
    seconds = seconds - 1;
    if(seconds === 0){
    if(obj && obj.action){
    obj.action();
    }else{
    $(flag).html("<span>0天</span><span>00:</span><span>00:</span><span>00</span>");
    }
    $(flag).removeAttr("data-go");
    clearInterval(intervalId);
    }else{
    var leftTime = __countDown.leftTimeToArr(seconds);
    $(flag).html("<span>" + leftTime["day"] + "天&nbsp;</span>"+"<span>" + leftTime["hour"] + ":</span><span>" + leftTime["minute"] +":</span><span>" + leftTime["second"] + "</span>");
    }

    },1000);
    }
    },
    leftTimeToArr:function(seconds){
    var lt = [],day1,hour,minute,second; day1=Math.floor(seconds/(60*60*24)); lt["day"] = day1; hour=Math.floor((seconds-day1*24*60*60)/3600); hour = __countDown.formatTimeNum(hour); lt["hour"] = hour; minute=Math.floor((seconds-day1*24*60*60-hour*3600)/60); minute = __countDown.formatTimeNum(minute); lt["minute"] = minute; second=Math.floor(seconds-day1*24*60*60-hour*3600-minute*60); second = __countDown.formatTimeNum(second); lt["second"] = second; return lt; }, formatTimeNum:function(num){ if(parseInt(num) < 10){ num = "0" + num; } return num } }; TC.countDown = __countDown ||{};})();
  • 相关阅读:
    网络负载均衡LVS
    JS 模仿红绿灯(控制台)
    【转】wrk 压力测试的 lua脚本
    linux开机 自动挂载和启动jar包
    【转】jprofiler linux配置需要监听的程序的端口
    时间复杂度总结
    Windows Subsystem for Linux (WSL) 安装
    敬畏用户
    Golang语言HTTP客户端实践
    Groovy入门常用语法
  • 原文地址:https://www.cnblogs.com/baoliwei/p/4607366.html
Copyright © 2011-2022 走看看