What is the modern way to do this?
Use an expression with the function name :
$(window).on('scroll', function handler(){ ... code ... if(code_was_successful){ $(window).off('scroll', handler); } });
.off () requires the selector to untie a specific handler
No no. Just like .on does not require a selector. You only need a selector if you want to cancel the delegated event handler.
As you can read in the .off documentation about the selector argument:
A selector that must match the first passed to .on () when attaching event handlers.
So, if you did not use it in .on , you do not use it in .off .
source share