The page has a textbox, when the user focus on it, I want to make a calculation every two seconds by the value of it. If the user blurs the textbox, I want to stop this calculation.
Assume only float values can be entered by the user. I tried something but it doesnt work.
Added original code, so if the fiddle gets removed we can still see what the issue was:
$(function() {
var newValue = "";
var makeCalculation = function(var1) {
newValue = var1 * 0.05;
};
$("#textBox").focus(function() {
(function() {
makeCalculation(parseFloat($(this.val())))
setTimeout(arguments.callee, 2000);
});
});
});
Check this fiddle: http://jsfiddle.net/TUNm4/6/
What you can also do, is onkeyup, then you always got the latest calculations and you don’t need the timeout: