1

I've had a look round and I'm a little confused.

There seems to be several responses to my particular problem but I don't fully understand.

So if anybody could help that'd be great!

Basically, I set a variable

var firstThumb = "02"   

And I want to increase it

function thumbIncrease() {
            firstThumb++
        };

on an interval

setInterval(function(){
            getThumb.trigger("click");
            thumbIncrease();
        }, 5000);

Basically what I expect to happen is - Every 5 seconds a click is triggered, and I want my firstThumb variable to increase by 1.

So 02, click, 03, click, 04, click....

So each time it clicks a different element.

Any help would be great!

Thanks!!

EDIT

Thanks for the responses! I kind of understand now! So the reason I've got a 0, is more for me than for the code. I currently have 15 things that I need clicking. 01 and 15 being 2 characters, meaning my code looks neater. Should I just man up and remove the 0? Or is there a better way of doing it?

I just like the way it lines up!!

8
  • 5
    Hint #1: "02" is a string, not a number Commented Sep 28, 2014 at 20:15
  • Well, that's a string. You'd have to increase the number and conditionally concatenate it with a leading '0' if you want exactly that. Commented Sep 28, 2014 at 20:16
  • the reason it doens't work is because your variable is a string you then trying to convert it to a number and then add a 0 to the front of it Commented Sep 28, 2014 at 20:16
  • 2
    @LeshaOgonkov There is no string concatenation going on here. Just type coercion. Commented Sep 28, 2014 at 20:16
  • 1
    Handling numbers is different to representing numbers, 1 is a number. "1" is a string. 01 is a number but will return as 1. For you to prefix with Zero you can use a second variable for your visual representation of the number and make it a string. Commented Sep 28, 2014 at 20:26

2 Answers 2

1

var counter = 1;

setInterval(function() {

  var _text = counter + "<br>";

  $("div").append(_text);

  counter++;   
   
},5000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
</div>

If you want it to replace the old number use .html() instead of .append()

"01" is a string, 1 is a number you can increment.

Sign up to request clarification or add additional context in comments.

Comments

0

If you just need zero padding:

function pad(num, size) {
    var s = num+"";
    while (s.length < size) s = "0" + s;
    return s;
}

var thumbNumber = "02";

thumbNumber = parseInt(thumbNumber);
var nextNumber = pad(++thumbNumber,2);

console.log(nextNumber);

Live Demo -- http://jsfiddle.net/v02vgw6w/2/

1 Comment

Just to point this out, these days there is String.prototype.repeat, though it might need shimming.

Your Answer

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.