码迷,mamicode.com
首页 > Web开发 > 详细

[JS原生]自己写的一个电商活动定时器

时间:2017-10-11 10:50:46      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:flash   实现   自己   dead   一段   active   parse   oda   cti   

 

 

假定电商活动有几个固定时间,通过写一段js自动循环实现。

还未完整测试过结果,先做一个记录~~~

 

 var activity = [2,6,10,14,18,22];

    var today = new Date();
    var nowHour = today.getHours();
    var deadline,active_activity;

    for(let i = 0; i<activity.length;i++){
      if(nowHour>=22||nowHour<2){
        deadline=2;
        active_activity=22;
      } else {
        if(nowHour>=activity[i]&&nowHour<activity[i+1]){
          deadline=activity[i+1];
          active_activity = activity[i];
        } 
      }
    } //判断当前是哪一个倒计时活动


    var now,deadlineTime;
    var Hour,Minute,Second;
    
    setInterval(function(){

      if(Hour===0&&Minute===0&&Second===1){
        deadline=deadline+4;
        if(active_activity<22){
          active_activity=active_activity+4;
        } else{
          active_activity=2;
        }
      }

      var today = new Date();

      now = today.getTime();

      var time = new Date(today.getFullYear()+‘/‘+(today.getMonth()+1)+‘/‘+today.getDate()+‘ ‘+deadline+‘:00:00‘).getTime();

      if(nowHour<22){
        deadlineTime = time;
      } else{
        deadlineTime = time + 24*60*60*1000;
      }

      var period = (deadlineTime - now)/1000;   

      Hour = parseInt(period/3600,10)>0?parseInt(period/3600,10):0;
      Minute = parseInt((period-3600*Hour)/60, 10)>0?parseInt((period-3600*Hour)/60, 10):0;
      Second = parseInt(period-3600*Hour-60*Minute,10)>0?parseInt(period-3600*Hour-60*Minute,10):0;

    $(".count-down-hour").html(Hour);
    $(".count-down-minute").html(Minute);
    $(".count-down-second").html(Second);


    },1000); //根据当前活动计算倒计时

    var active_activity_word;

    if(active_activity>6){
      active_activity_word=active_activity;
    } else{
      active_activity_word=‘0‘+active_activity;
    }

    $(".flash-sale-activity").html(active_activity_word); 



  });



}

 

[JS原生]自己写的一个电商活动定时器

标签:flash   实现   自己   dead   一段   active   parse   oda   cti   

原文地址:http://www.cnblogs.com/espelansa/p/7648655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!