Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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>



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.
Posted

1 solution

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
 
Share this answer
 
Comments
Maciej Los 28-Oct-14 17:32pm    
Short and to the point!
+5!
Sergey Alexandrovich Kryukov 28-Oct-14 17:40pm    
Thank you, Maciej.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900