gpt4 book ai didi

javascript 计时器在给定条件时返回 NaN

转载 作者:行者123 更新时间:2023-11-28 08:18:09 25 4
gpt4 key购买 nike

<%@ 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 秒计时器。我给出了一个条件 if(sec==0) 意味着还要运行 30 秒,但是问题是什么意味着它显示 0 的 NaN,这是我给出的条件。所以请帮助我如何在没有 NaN 的情况下进行操作。所以计时器希望在前 60 秒运行,并在 30 秒后运行而不显示 NaN(不是数字)。

最佳答案

  1. setTimeout函数的第一个参数是一个函数或函数的名称(不是调用)。所以你应该替换所有此类行:

    timerID = setTimeout("cd()", 100); 通过:timerID = setTimeout(cd, 100);

    关于setTimeout函数的更多详细信息,请引用here .

  2. 由于某种原因,在您的 cdd 函数中出现以下行:

    var sec = diff.getSeconds() - 29;

    返回负值并在此行之后:

    秒 = "0"+ 秒;

    sec 的值变为等于 0-*数字*(例如,"0-10"),其中不是你所说的数字。

    可能的解决方案是使用 Math.abs() 获取 sec 变量的绝对值。就像这样:

    var sec = Math.abs(diff.getSeconds() - 29);

关于javascript 计时器在给定条件时返回 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23357509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com