The short version: "How can I see all the events registered with $(window).on("unload")
?"
For instance, if I did this: $(window).on("unload",doSomething())
, how could I get a list of all the unload events, including doSomething()
?
Part 2: If I had more than one function registered to the unload event, How would I go about removing only one instead of all of them?
The longer version: if you're dying to know...
I am working on an application where users may create thousands of records at a time.
While they're on the 'create records' page I perform client-throttling, pushing 25 records at a time. However, if they leave the page, I'd like to push the remaining records to the server so they can be created while the user does other stuff.
To accomplish this, I recurse through the creation function, allowing an interruption:
function recurseCreate( createRemaining = 0 ){
if(createRemaining > 0)
$.ajax(url:url,method:'POST', data:{qty:createRemaining});
else {
$(window).on("unload", function(){
recurseCreate( desiredQty - throttleQty );
});
$.ajax(url:url,method:'POST', data:{qty:throttleQty}).always(()=>{
$(window).off("unload"); // remove the listener in case all the items are created
});
}
And this seems to work, my only concern is $(window).off("unload")
- this is a small part of a much larger whole, so I don't know if there's other events waiting for the unload event to trigger them.
How can I tell if I'm removing other events with $(window).off("unload")
? And, if I would be removing more than one function with this, how would I only remove one?