0

I have a basic counter that counts up within an input field. this works exactly how I want it to. The problem I am facing is that I can only set the target number via the input value. i want to be able to set this via a js variable, ignoring the html input value

DEMO http://jsfiddle.net/vyN6V/238/

Current jQuery

  var number = 827;

    function count(Item){
        var current = parseInt(Item.val());
        Item.val(current +=5);
        if(current < number){
            setTimeout(function(){count(Item)}, 0.1);
        }
    }        
    count($(".input"));

Desired jQuery (doesn't work)

  var number = 827;
  var aValue = 500;

    function count(Item){
        var current = aValue;
        Item.val(current +=5);
        if(current < number){
            setTimeout(function(){count(Item)}, 0.1);
        }
    }        
    count($(".input"));

4 Answers 4

3

Should work, you just forgot to add 5 to aValue:

    var current = aValue;
    aValue+=5;
Sign up to request clarification or add additional context in comments.

Comments

2

Your current is inside your function, it works if you simply use aValue:

Item.val(aValue += 5);

Fiddle

Comments

0

this would solve the problem: http://jsfiddle.net/Jg6LN/1/

var number = 827;
  var aValue = 500;

    function count(Item, value){
        var current = value || parseInt(Item.val());
        Item.val(current +=5);
        if(current < number){
            setTimeout(function(){count(Item)}, 0.1);
        }
    }        
    count($(".input"), aValue);

Comments

0

What about this?

var aValue = 500;
var number = 827;

function count(Item) {
    var current = parseInt(Item.val()) || aValue;
    Item.val(current + 5);

    if (Item.val() < number) {
        setTimeout(function () {
            count(Item)
        }, 0.1);
    }
}

count($(".input"));

http://jsfiddle.net/vyN6V/242/

Comments

Your Answer

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