美文网首页
倒计时方法

倒计时方法

作者: 伊米萱 | 来源:发表于2016-12-02 18:03 被阅读0次

    servertiem 现在时间 datatime 目标时间

            function timeCount(servertime,datatime,obj){            
                if(datatime<servertime){
                    $(".countdown").hide();
                    clearInterval(timer);
                    return false;
                } 
                var localtime = new Date();
                var timedifference = localtime-servertime;
                var timecount = interval(timedifference,datatime);
                var timer = setInterval(function(){
                    timecount = interval(timedifference,datatime);
                    if (!timecount){
                        clearInterval(timer);
                        $(".countdown").hide();
                        return false;
                    }
                    else{
                        //代码剪切填充,例如
                        obj.find("span").eq(0).html(timecount.charAt(0));
                        obj.find("span").eq(1).html(timecount.charAt(1));
                        obj.find("span").eq(2).html(timecount.charAt(2));
                        obj.find("span").eq(3).html(timecount.charAt(3));
                        obj.find("span").eq(4).html(timecount.charAt(4));
                        obj.find("span").eq(5).html(timecount.charAt(5));
                    }
                },1000) 
            }
    
    获取时间差函数并转换为:天时分秒
         function interval(timedifference,datatime){
            var timecount = parseInt(datatime) + timedifference - new Date();
            if(timecount<=0)return false;
            var int_day, int_hour, int_minute, int_second;
            int_day = Math.floor(timecount/86400000);
            int_hour = Math.floor(timecount%86400000/3600000);
            int_minute = Math.floor(timecount%3600000/60000);
            int_second = Math.floor(timecount%60000/1000);
                int_hour = int_day*24+int_hour;
                if(int_hour < 10)
                int_hour = "0" + int_hour;
                if(int_minute < 10)
                int_minute = "0" + int_minute;
                if(int_second < 10)
                int_second = "0" + int_second;
                var result = ""+int_hour+int_minute+int_second;
             //一定要加空字符串,否则是数字相加而不是拼接了
                return result;
         }
    
    

    相关文章

      网友评论

          本文标题:倒计时方法

          本文链接:https://www.haomeiwen.com/subject/adjqmttx.html