基本上我对javascript很擅长,我想为我的项目提供更好的倒数计时器。目前我的计时器看起来像这样:
1时25分05秒
我希望它看起来像这样:
1小时25分5秒
但我也希望它聪明一点。因此,如果只有25分钟就会说:
25分钟。
这是我目前的javascript代码:
function secondCountdown(){
if(typeof(skip) == "undefined"){
skip = "set";
} else {
var timeleft = document.getElementById("timeleft").innerHTML;
var time_split = timeleft.split(":");
if(parseInt(time_split[0]) < 10){
time_split[0] = time_split[0].charAt(1);
}
if(parseInt(time_split[1]) < 10){
time_split[1] = time_split[1].charAt(1);
}
if(parseInt(time_split[2]) < 10){
time_split[2] = time_split[2].charAt(1);
}
seconds = parseInt(time_split[2]) + parseInt(time_split[1]*60) + parseInt(time_split[0]*3600);
seconds -= 1;
if(seconds > 0){
var hours= Math.floor(seconds/3600);
seconds %= 3600;
var minutes = Math.floor(seconds/60);
seconds %= 60;
var timeleft = ((hours < 10) ? "0" : "") + hours + ":" + ((minutes < 10) ? "0" : "") + minutes + ":" + ((seconds < 10) ? "0" : "") + seconds;
document.getElementById("timeleft").innerHTML = timeleft;
} else {
document.getElementById("timeleft").innerHTML = "";
window.location='test.php?pageid=' + getURLParam("pageid");
return false;
}
}
setTimeout("secondCountdown()",1000);
}
window.onload = secondCountdown;
也许有人可以为我编辑它以使其输出我想要的内容?
编辑:
这是计时器的PHP端。
<span id="timeleft">
$master = $timer['gta_time'] - time();
echo date( "00:i:s", $timer['gta_time'] - time() );
</span>
答案 0 :(得分:0)
而不是这一行:
var timeleft = ((hours < 10) ? "0" : "") + hours + ":" + ((minutes < 10) ? "0" : "") + minutes + ":" + ((seconds < 10) ? "0" : "") + seconds;
为基本解决方案说出这样的话:
var timeleft = (hours > 0 ? hours + " hours " : "")
+ (minutes > 0 ? minutes + " minutes " : "")
+ (seconds > 0 ? seconds + " seconds " : "");
或完整:
var timeleft = "";
if (hours > 0)
timeleft += hours + (hours > 1 ? " hours" : " hour");
if (minutes > 0) {
if (hours > 0)
timeleft += seconds > 0 ? ", " : " and ";
timeleft += minutes + (minutes > 1 ? " minutes" : " minute");
}
if (seconds > 0) {
if (timeleft != "")
timeleft += " and ";
timeleft += seconds + (seconds > 1 ? " seconds" : " second");
}
答案 1 :(得分:0)
好的,我现在就做了,它有效(我在Opera中测试过):
Date.prototype.toTimeString = function()
{
var toReturnTime = new Array();
var times = [this.getHours(), this.getMinutes(), this.getSeconds()];
var times_names = [' hour', ' minute', ' second'];
var tmp = null;
for (var i=0; i<times.length; i++)
{
tmp = times[i];
if (tmp > 0)
{
toReturnTime.push(tmp + times_names[i] + (tmp == 1 ? '' : 's'));
toReturnTime.push(', ');
}
}
if (toReturnTime.length/2 >= 2)
{
toReturnTime.pop();
tmp = toReturnTime.pop();
toReturnTime.pop();
toReturnTime.push(' and ' + tmp);
}
else
toReturnTime.pop();
return toReturnTime.join('');
}
var date, timeleft;
function secondCountdown()
{
date.setSeconds(date.getSeconds() - 1);
timeleft.innerHTML = date.toTimeString();
if ((date.getHours() + date.getMinutes() + date.getSeconds()) > 0)
setTimeout("secondCountdown()",1000);
else
timeleft.innerHTML = 'ITS OVER';
}
function init()
{
timeleft = document.getElementById("timeleft");
var time_split = timeleft.innerHTML.split(":");
date = new Date(0, 0, 0, parseInt(time_split[0]), parseInt(time_split[1]), parseInt(time_split[2]));
secondCountdown();
}
window.onload = init;
答案 2 :(得分:0)
噢,刚刚看到我对此有点慢,但似乎有效,所以无论如何我都会添加它。 :)
function parseTime() {
var timeLeftStr;
var timeLeft = 0;
timeLeftStr = document.getElementById("timeleft").innerHTML;
timeLeftStr.replace(/(\d+):(\d+):(\d+)/, function () {
for (var i = 1; i < arguments.length - 2; i++) {
// Convert to ms
timeLeft += arguments[i] * Math.pow(60, 3 - i) * 1000;
}
});
countdown(new Date(timeLeft));
}
function countdown(timeLeft) {
var hours = timeLeft.getUTCHours();
var minutes = timeLeft.getUTCMinutes();
var seconds = timeLeft.getUTCSeconds();
if (timeLeft.valueOf() == 0) {
document.getElementById("timeleft").innerHTML = "";
window.location = 'test.php?pageid=' + getURLParam("pageid");
return false;
} else {
document.getElementById("timeleft").innerHTML =
(hours == 0 ? "" : hours + (hours == 1 ? " hour" : " hours")) +
(minutes == 0 ? "" : (hours ? (seconds ? ", " : " and ") : " ") + minutes + (minutes == 1 ? " minute" : " minutes")) +
(seconds == 0 ? "" : (hours || minutes ? " and " : "") + seconds + (seconds == 1 ? " second" : " seconds"));
setTimeout(function () { countdown(new Date(timeLeft - 1000)); }, 1000);
}
}
window.onload = parseTime;
修改以删除skip
。
编辑2 以删除开始&amp;对正则表达式进行结束检查,添加,
和and
支持。
编辑3 以使用getUTCHours
等,因此它适用于其他时区(oops)。