Heim > Fragen und Antworten > Hauptteil
startClock();
var clock = null;
var clockRunning = false;
function showTime() {
var endTime = new Date("June 30,2017 23:59:59");
var nowTime = new Date();
var time = endTime - nowTime;
var $day = parseInt(time/(1000*60*60*24));
var $hour = endTime.getHours() - nowTime.getHours();
var $minute = endTime.getMinutes() - nowTime.getMinutes();
var $second = endTime.getSeconds() - nowTime.getSeconds();
if($second < 0) {
$second = 60 + $second;
$minute = $minute - 1;
}
if($minute < 0) {
$minute = 60 + $minute;
$hour = $hour - 1;
}
if($hour < 0) {
$hour = 24 + $hour;
$day = $day - 1;
}
$(".day").find("b").html($day);
$(".hour").html($hour);
$(".minute").html($minute);
$(".second").html($second);
}
function stopClock() {
if(clockRunning)
clearInterval(clock);
clockRunning = false;
}
function startClock() {
clock = setInterval('showTime()', 1000);
}
大家讲道理2017-04-11 11:33:22
自测OK。题主可以检查下自己的其他部分代码,是不是second类名写错?描述不变?
另外,代码显然有优化空间,至少endtime不需要每次都声明,至少有了time就不需要每次执行两次getHours()/getMinutes()/getSeconds()。代码至少砍掉一半。
https://codepen.io/laoli/pen/...
ringa_lee2017-04-11 11:33:22
//clock = setInterval('showTime()', 1000);
// 改成试试吧,虽然是一样的
clock = setInterval(showTime, 1000);