There are a few things that really bother me about how jquery handles nested functions (not to the extent that I can't sleep, but it gets there), and I want the jquery expert to explain how everything works me piece of the mind.
Let's say you have the code below:
<button id="first">click me first</button> <button id="second">click me next</button>
And the following jquery code:
$(document).ready(function() { $('#first').click(function() { $('#second').click(function() { alert('test'); }); }); });
A dialog box will appear if you click the first button and then the second button.
I understand that jquery creates the $('#first').click() function when the DOM is ready and calls it when someone clicks the first button.
However, I am puzzled that: [Q1] is the function $('#second').click() , also created when the DOM is ready or only when $('#one').click() ?
Now, when you look at jQuery code, there is nothing that โstores usโ in the function $('#first').click() , that is, when the user presses the first button, the function $('#second').click() must be created and we must immediately exit the function $('#one').click() . However, after pressing the first button, the jquery must somehow hold $('#second').click() indefinitely in memory if the user presses the second button.
[Q2] how does jquery know to save the $('#second').click() function in memory until the user presses the second button after pressing the first button?
Finally, let's say you wanted to change your code so that the user presses the second button within 10 seconds after pressing the first button to display a dialog box:
[Q3], how would you implement this so that jQuery knows to stop listening for click events on the second button after 10 seconds?