Related: Jquery timeout for doubleclick
Given this code:
function handleClick() {
var div = $(this);
var original = $(this).html();
div.html("Tap again");
$(".onlyfire").addClass("fire").off("click");
$(".fire").one("click", function(fire) {
alert("this should not be showing once the text is changed back to original");
$(this).off("click")
});
setTimeout(function() {
$(div).html(original);
$(".onlyfire").removeClass("fire").click(handleClick);
}, 2000);
}
function onlyfire() {
$(".onlyfire").click(handleClick);
};
onlyfire();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div class="onlyfire">
Do Something
</div>
the fire
className
is removed from .onlyfire
element following 2000ms
, where expected result is for click
event which was attached to .fire
would also be rendered inapplicable, as there would not be a .fire
element within document
.
However, as pointed out @120382833 at comment
Click on Do something, wait for 2 seconds until the Do Something is back, Click again and it will show the alert.. And it shouldn't.. It should display tap again and only if you click in first 2 seconds the alert should show up.
To reproduce
- Inspect
.onlyfire
element atconsole
; - click
.onlyfire
element; - do nothing for two seconds;
- the original text should be set back at
.onlyfire
element; .fire
className
will be removed from.onlyfire
element;- click
.onlyfire
element again - handler attached to
.fire
element is called, in turn callingalert("this should not be showing once the text is changed back to original")
, though no.fire
element exists indocument
at that point
Question: Is this a jQuery bug, or expected behaviour? Or, have we overlooked something within javascript
which would explain result?