Reputation:
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"));
Upvotes: 0
Views: 130
Reputation: 361
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/
Upvotes: 0
Reputation: 13598
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);
Upvotes: 0
Reputation: 7377
Your current is inside your function, it works if you simply use aValue
:
Item.val(aValue += 5);
Upvotes: 2
Reputation: 16068
Should work, you just forgot to add 5 to aValue:
var current = aValue;
aValue+=5;
Upvotes: 3