Error In Javascript Closure
I have a modal dialog with a submit button, which when clicked caused the following code to execute: $('#addqueuebutton').on('click',function(){ var counter = 0; return fu
Solution 1:
You're assigning the wrong function to click
. When you click, you initialise counter
and then return the inner function.
You need to call the outer function and assign its return value to the second argument of on()
.
});
should be }());
Or, to make it clearer:
functioncreate_counter(){
var counter = 0;
returnfunction(){
counter += 1;
...
alert(counter);
};
}
var counter_incrementing_function = create_counter()
$("#addqueuebutton").on("click", counter_incrementing_function);
Post a Comment for "Error In Javascript Closure"