我改变了我的代码,但它没有显示秒数倒计时为什么?
我有这个javascript代码:
I have this javascript code:
<!DOCTYPE html>
<html>
<head>
<title>change picture</title>
<script type = "text/javascript">
var counter = 7000;
function displayNextImage() {
x = (x === images.length - 1) ? 0 : x + 1;
document.getElementById("img").src = images[x];
}
function displayPreviousImage() {
x = (x <= 0) ? images.length - 1 : x - 1;
document.getElementById("img").src = images[x];
}
function startTimer() {
counter = setInterval(displayNextImage, counter);
var count = 60;
count = count - 1;
if (count == -1) {
clearInterval(counter);
return;
}
counter = count % 60;
var minutes = Math.floor(count / 60);
var hours = Math.floor(minutes / 60);
minutes %= 60;
hours %= 60;
document.getElementById("startTimer").innerHTML = hours + " Hours " + minutes + " Minutes and " + seconds + " Seconds left untill the next news update.";
}
var images = [], x = -1;
images[0] = "http://newsxpressmedia.com/files/theme/radar000116.Gif";
images[1] = "http://newsxpressmedia.com/files/theme/radarClosed.gif";
</script>
<br><br><span id="startTimer"></span><br><br>
</head>
<body onload = "startTimer()">
<img id="img" src="http://newsxpressmedia.com/files/theme/radar000005.Gif">
<button onclick="displayPreviousImage()">Previous</button>
<button onclick="displayNextImage()">Next</button>
</body>
</html>
变量计数器全局设置为7秒。当我加载我的网站时,它确实每7秒更换一次图像。
但是我想在计时器中显示7秒钟回来的网站。这次是7秒但是如果我设置计数器例如50000那么它应该倒数5分钟并且每次也显示秒数倒计时。
我如何制作它可以与一个全局变量计数器一起工作吗?
如果我将使用3个全局变量几小时分钟,我怎样才能使它工作?
在两种方式中,结果都应相同,以显示计数器倒计时,直到下一张图像。在我的代码中重复的地方我将在稍后修复它,但首先我希望它能够正常工作。
The variable counter is global set to 7 seconds. When i'm loading my website it does changing the images every 7 seconds.
But i want to show in the timer to display on the website the 7 seconds ocunting back. This time it's 7 seconds but if i set counter to for example 50000 then it should count back 5 minutesand show each time also the seconds counting back.
How do i make it to work with one global variable counter ?
How can i make it to work also if i will use 3 global variable for hours minutes seconds ?
In both way the result should be the same to display the counter counting back untill the next image. And places in my code that are repeated i will fix it later but first i want it to work at all.
仔细看看。您的代码不显示对象的定义和初始化seconds
,但它在函数的最后一行使用startTimer()
。机会是,这是一个未定义的对象。如果您在某处定义它,请找出它并为其分配适当的值。由于我看不到它,我无法解决它。 :-)-SA
Look thoroughly. Your code doesn't show the definition and initialization of the objectseconds
, but it is used in the last line of the functionstartTimer()
. Chances are, this is an undefined object. If you defined it somewhere, please find it out and assign proper value to it. As I cannot see it, I cannot fix it. :-)—SA