国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

js實現(xiàn)網(wǎng)頁同時進行多個倒計時功能

2024-05-06 15:42:09
字體:
來源:轉載
供稿:網(wǎng)友

本文實例為大家分享了js實現(xiàn)網(wǎng)頁同時進行多個倒計時的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)建一個時間類Timer.

每個商品的倒計時生成一個實例:var time = new Timer();

/***startime 應該是毫秒數(shù)*/ var Alarm = function (startime, endtime, countFunc, endFunc) {    this.time = Math.floor((endtime - startime) / 1000); //時間    this.countFunc = countFunc; //計時函數(shù)    this.endFunc = endFunc; //結束函數(shù)    this.flag = 't' + Date.parse(new Date()); //  };Alarm.prototype.start = function () {  var self = this;   self.flag = setInterval(function () {    if (self.time < 0) {      clearInterval(self.flag);      self.endFunc();      console.log('計時結束');    } else {      var minute, hour, day, second;      day = Math.floor(self.time / 60 / 60 / 24) < 10 ? '0' + Math.floor(self.time / 60 / 60 / 24) : Math.floor(self.time / 60 / 60 / 24);      hour = Math.floor(self.time / 60 / 60 % 24) < 10 ? '0' + Math.floor(self.time / 60 / 60 % 24) : Math.floor(self.time / 60 / 60 % 24);      minute = Math.floor(self.time / 60 % 60) < 10 ? '0' + Math.floor(self.time / 60 % 60) : Math.floor(self.time / 60 % 60);      second = Math.floor(self.time % 60) < 10 ? '0' + Math.floor(self.time % 60) : Math.floor(self.time % 60);      //倒計時執(zhí)行函數(shù)      self.countFunc(second, minute, hour, day);      self.time--;    }  }, 1000);}

如果調(diào)用:

var time1 = new Alarm(startime, endtime, countFunc, endFunc);time1.start(); var time2 = new Alarm(startime, endtime, countFunc, endFunc);time2.start();...

調(diào)用示例:

var countTime = function () {  var eles = $('.count_time'),    len = eles.length;  for (; len > 0; len--) {    var ele = eles.eq(len - 1);    (function (ele) {      startTime = new Date(ele.attr('data-start-time')).getTime(),          endTime = new Date(ele.attr('data-end-time')).getTime(),          alarm = new Alarm(startTime, endTime, function (second, minute, hour, day) { //計時鐘            ele.text(hour + ':' + minute + ':' + second);          }, function () { //倒計時結束            ele.text('00:00:00');            window.location.reload();          });      alarm.start();    })(ele);  }};countTime();

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 定结县| 双辽市| 宜兰县| 镇宁| 乌兰察布市| 八宿县| 西昌市| 锡林浩特市| 贡嘎县| 涿州市| 惠东县| 汶川县| 都匀市| 梁河县| 中江县| 禹州市| 甘南县| 德惠市| 民乐县| 自治县| 五寨县| 铜陵市| 麟游县| 兰州市| 新巴尔虎左旗| 刚察县| 古交市| 汾西县| 东丰县| 辛集市| 馆陶县| 永吉县| 若羌县| 和田县| 平利县| 衡南县| 札达县| 微博| 阿拉尔市| 尼勒克县| 宜川县|