I've a function for window scroll like this:
function fixDiv() {
//scroll script goes here
}
$(window).scroll(fixDiv);
Now I want to cancel the function when the button is clicked
$('button').on('click',function(){
//cancel the fixDiv() function
// I have tried this:
fixDiv != fixDiv; // and changed/set variable for fixDiv() like var fixDiv = function(){}
});
But seems not working. What way can I cancel the function.
(please note: if again clicked on the button that should be functioning again)
please read the bounty description.
Try something like that:
DEMO
$('button').on('click', function () {
this.off = !this.off;
$(window)[this.off?'off':'on']("scroll", fixDiv )
});
UPDATE
Now if you want to override the function itself, you could use following code:
//using anonymous function as handler, code inside is processing on the fly
$('#anchorAnonymous').on('click',function(){
fixDiv();
//any other code, still be called even fixDiv() is overrided...
});
//click to override fixDiv() function
$('#btnoverride').on('click', function () {
this.override = !this.override;
//$.noop = function(){} -> do nothing
fixDiv = this.override?$.noop:storedFixDiv;
});
//this is used to store fixDiv() function
window.storedFixDiv = fixDiv;
Beware, overriding the function won't override the handler if used as referenced method. Because jQuery will create a handler for each event stored in $._data(elem,"events")
.
To illustrate it, see difference in jsfiddle:
jsFiddle example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With