猿问

求到每周二的一个倒计时代码

从8月1号开始到12月31号结束,
求当前时间到每个周二早上9点钟的一个倒计时

桃花长相依
浏览 558回答 1
1回答

人到中年有点甜

其实你这个问题主要就是目标时间与当前时间差的问题。目标时间你是可以计算出来的,规定一个初始时间,7天为一个循环;而当前时间你可以直接从服务器获取。下面简单写了一段,你可以试试:&nbsp;//目标时间规定&nbsp; &nbsp; var newTimer = new Date("2016/08/02 09:00:00");&nbsp; &nbsp; newTimer = newTimer.getTime();&nbsp; &nbsp; function time() {&nbsp; &nbsp; &nbsp; &nbsp; //当前时间获取(可以从服务器获取)&nbsp; &nbsp; &nbsp; &nbsp; var nowTimer = new Date();&nbsp; &nbsp; &nbsp; &nbsp; nowTimer = nowTimer.getTime();&nbsp; &nbsp; &nbsp; &nbsp; //如果当前时间大于目标时间,则加上一周;直到目标时间大于当前时间&nbsp; &nbsp; &nbsp; &nbsp; while (newTimer<=nowTimer){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newTimer = newTimer + 7 * 24 * 60 * 60 * 1000;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //以下为时间计算显示&nbsp; &nbsp; &nbsp; &nbsp; var spanTimer = newTimer - nowTimer;&nbsp; &nbsp; &nbsp; &nbsp; if (spanTimer > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var day = Math.floor(spanTimer / 86400000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spanTimer -= day * 86400000;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var hour = Math.floor(spanTimer / 3600000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spanTimer -= hour * 3600000;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var minute = Math.floor(spanTimer / 60000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spanTimer -= minute * 60000;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var second = Math.floor(spanTimer / 1000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(day + '天' + hour + '时' + minute + '分' + second + '秒')&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //定时器循环&nbsp; &nbsp; timer = window.setInterval(function () {&nbsp; &nbsp; &nbsp; &nbsp; time();&nbsp; &nbsp; }, 1000);至于你要的到12月31日停止这个问题,做个判断把定时器干掉就ok了!
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答