您的位置:首页 > Web前端 > JavaScript

javascript倒计时代码

2015-10-17 21:53 489 查看
其实就是用两个时间戳相减,余数转换为日期,就是所剩的年月日时分秒,不过年份-1970

$scope.timerID = null;
$scope.timerRunning = false;
$scope.showtime = function() {
if ($scope.data === undefined || $scope.data.auction_time_end === undefined) {
return;
}
Today = new Date();
var diffs = $scope.data.auction_time_end - Math.round(new Date().getTime()/1000);
if (diffs < 0) {
$("#spanLeft").html('0年, 0月, 0天, 0小时, 0分, 0秒');
return;
}
var newDate = new Date(diffs * 1000);
var year = newDate.getFullYear() - 1970;
var month = newDate.getMonth();
var date = newDate.getDate();
var hour = newDate.getHours();
var minutes = newDate.getMinutes();
var seconds = newDate.getSeconds();
s=year+'年, '+month+'月, '+date+'天, '+hour+'小时, '+minutes+'分, '+seconds+'秒';

$("#spanLeft").html(s);

$scope.timerID = setTimeout($scope.showtime, 1000);
$scope.timerRunning = true;
}
$scope.timerID = null;
$scope.timerRunning = false;
$scope.stopclock = function() {
if($scope.timerRunning)
clearTimeout($scope.timerID);
$scope.timerRunning = false;
}

$scope.startclock = function() {
$scope.stopclock();
$scope.showtime();
}
$scope.startclock()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: