1

I'm trying to make a counter that goes up to 100 in 10 seconds and then loops. What is wrong with my code?

window.onload = function () {
 var showPercent = window.setInterval(function () {
 if (currentPercent < 100) {
 currentPercent += 1;
 } else {
 currentPercent = 0;
 }
 document.getElementById('result').innerHTML = currentPercent;
 }, 100);
};

http://jsfiddle.net/JBVA2/

Xotic750
23.6k8 gold badges59 silver badges81 bronze badges
asked Feb 27, 2014 at 23:24
5
  • First tip: see declaring variables with Javascript's var Commented Feb 27, 2014 at 23:26
  • 1
    Second tip: look in the console and you will see Uncaught ReferenceError: currentPercent is not defined Commented Feb 27, 2014 at 23:29
  • 2
    Third tip: Your code doesn't even execute because you are assigning to window.onload after the page was already loaded (the jsFiddle is set up to run the JavaScript "onLoad"). Commented Feb 27, 2014 at 23:30
  • Indeed: consider setting no wrap - in <body> in your jsFiddle options. Commented Feb 27, 2014 at 23:31
  • Fourth tip: In your case it doesn't matter but see Security Considerations when using .innerHTML Commented Feb 27, 2014 at 23:39

4 Answers 4

4

You're not declaring currentPercent anywhere on your code, you should add something like: var currentPercent = 0; before showPercent

JSBin Demo

answered Feb 27, 2014 at 23:27
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, five seconds XD +1
@NiettheDarkAbsol you're pretty fast I was just lucky this time :)
0
var currentPercent = 0; 
setInterval(function() {
 if (currentPercent < 100) {
 currentPercent += 1;
 } else {
 currentPercent = 0;
 }
 document.getElementById('result').innerHTML = currentPercent;
}, 100);
answered Feb 27, 2014 at 23:33

Comments

0

In your fiddle you already had it to run onload. The problem with your snippet is that currentPercent needs to exist outside the setInterval callback scope:

var currentPercent = 0;
var showPercent = window.setInterval(function() {
 if (currentPercent < 100) {
 ++currentPercent;
 } else {
 currentPercent = 0;
 }
 document.getElementById('result').innerHTML = currentPercent;
}, 100);

http://jsfiddle.net/JBVA2/4/

answered Feb 27, 2014 at 23:35

Comments

0

I got the following error when loading the page in Firefox with Firebug enabled:

ReferenceError: currentPercent is not defined

You're comparing the value stored in variable currentPercent against '100', but the variable hasn't been defined yet. Making a couple of small alterations to the code makes it work just fine:

 var currentPercent = 0;
 window.onload = function() {
 var showPercent = window.setInterval(function() {
 if (currentPercent < 100) {
 currentPercent += 1;
 }
 document.getElementById('result').innerHTML = currentPercent;
 }, 100);
 };
answered Feb 27, 2014 at 23:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.