我改变了我的代码,但它没有显示秒数倒计时为什么? [英] I changed my code but it dosen't show the seconds counting down why ?

查看:77
本文介绍了我改变了我的代码,但它没有显示秒数倒计时为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个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 object seconds, but it is used in the last line of the function startTimer(). 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


这篇关于我改变了我的代码,但它没有显示秒数倒计时为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆