I am having a drop down as follows.
<select id="mySelect"> <option value="one">one</option> <option value="two">Two</option> </select>
I am having a change event handler which gets invoked when user selects Two, which is as follows .
$('select#mySelect').on('change',function(){
if(//Two is selected){
display_message("Processing....<a id='manual'>Try Manually</a>");
//an ajax call which gets the content from server
//if successful display another drop down using the content retrieved from server
//else
display_message("Processing failed...<a id='manual'>Try Manually</a>");
}else{
$('div#content').html("<input type='text' name='name' />");
}
});
Also, I am having another click event handler which gets invoked when user clicks on Try Manually, which is as follows.
$('#manual').on('click',function(){
$('div#content').html("<input type='text' name='name' />");
});
So, when user selects Two ,then a message – ‘Processing…TryManually’ gets displayed and then after successful ajax call a drop down gets displayed.
If ajax call took long time then message stays on screen. During this time if user clicks on Try Manually, then before displaying text box I should cancel the running ‘change’ event handler.
How to achieve this ?
Advertisement
Answer
You could have a global variable, so that you can cancel the event if it is onscreen, like this:
var disableEvent = false;
$('select#mySelect').on('change',function(){
if (disableEvent) return false;
if(//Two is selected){
display_message("Processing....<a id='manual'>Try Manually</a>");
disableEvent = true;
//an ajax call which gets the content from server
//if successful display another drop down using the content retrieved from server
//else
display_message("Processing failed...<a id='manual'>Try Manually</a>");
disableEvent = false;
}else{
$('div#content').html("<input type='text' name='name' />");
}
});