<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="jquery-1.10.2.js"></script>
<script language="JavaScript">
var hoursleft = 0;
var minutesleft = 0; // you can change these values to any value greater than 0
var secondsleft = 0;
var millisecondsleft = 0;
var finishedtext = "Your Time is Up Question Switch to Others!"; // text that appears when the countdown reaches 0
end = new Date();
//end.setHours(end.getHours()+hoursleft);
end.setMinutes(end.getMinutes() + minutesleft);
end.setSeconds(end.getSeconds() + secondsleft);
end.setMilliseconds(end.getMilliseconds() + millisecondsleft);
function cd() {
now = new Date();
diff = end - now;
diff = new Date(diff);
var msec = diff.getMilliseconds();
var sec = diff.getSeconds() ;
var min = diff.getMinutes();
//var hr = diff.getHours() - 1;
if (min < 10) {
min = "0" + min;
}
if (sec < 10) {
sec = "0" + sec;
}
if (msec < 10) {
msec = "00" + msec;
} else if (msec < 100) {
msec = "0" + msec;
}
if (sec== 0) {
//clearTimeout(timerID);
//document.getElementById("cdtime").innerHTML = finishedtext;
//alert("Your time is Up Switch to Others");
timerID = setTimeout("cdd()", 100);
//$("#cdtime").slideUp( 300 ).delay( 8000 ).fadeIn( 400 );
} else {
document.getElementById("cdtime").innerHTML = +sec;
timerID = setTimeout("cd()", 100);
} // you can leave out the + ":" + msec if you want...
// If you do so, you should also change setTimeout to setTimeout("cd()",1000)
}
function cdd() {
now = new Date();
diff = end - now;
diff = new Date(diff);
var msec = diff.getMilliseconds();
var sec = diff.getSeconds() -29;
var min = diff.getMinutes();
//var hr = diff.getHours() - 1;
if (min < 10) {
min = "0" + min;
}
if (sec < 10) {
sec = "0" + sec;
}
if (msec < 10) {
msec = "00" + msec;
} else if (msec < 100) {
msec = "0" + msec;
}
if (sec == 0) {
clearTimeout(timerID);
confirm("Time is over");
} else {
document.getElementById("cdtime").innerHTML = +sec;
}
timerID = setTimeout("cdd()", 100);
}
window.onload = cd;
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<span id="cdtime"></span>
</body>
</html>
嗨我需要在60秒后制作计时器60秒,另外30秒需要在网页上运行。在那里我给出了一个条件,如果(秒== 0)意味着另外30秒想要运行,但问题是什么意味着它显示NaN为0,我给出了条件。所以请帮助我如何在没有NaN的情况下做。所以计时器要先运行60秒,30秒后不要显示NaN(不是数字)。
答案 0 :(得分:0)
setTimeout
函数的第一个参数是函数或函数的名称(不是调用)。所以你应该替换所有这些行:
timerID = setTimeout("cd()", 100);
:timerID = setTimeout(cd, 100);
有关setTimeout
功能的详细信息,请参阅here。
出于某种原因,在cdd
函数中有以下行:
var sec = diff.getSeconds() - 29;
返回负值并在此行之后:
sec = "0" + sec;
sec
的值等于0-*the number*
(例如"0-10"
),这不是您所说的数字。
可能的解决方案是使用Math.abs()
来获得sec
变量的绝对值。就像这样:
var sec = Math.abs(diff.getSeconds() - 29);