0

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?

Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
  • I'm just wondering if there's a way to see all events registered to unload, not looking for a change to my solution, but thanks. – Travis Heeter Jan 09 '19 at 17:26
  • That's fine if you say it's not possible, but I mean it's possible to attach multiple things to the unload event, right? I assume it's possible because you can attach multiple event listeners to DOM elements, but I'm not sure if `Window` or `Document` act the same. And if you can, how can you tell what's attached? I mean how can you tell what's attached when you have multiple things attached to a DOM element? Is it even possible? – Travis Heeter Jan 09 '19 at 19:37
  • Searching SO for 'list jquery events' gave this likely answer: https://stackoverflow.com/a/2008622/2181514 – freedomn-m Jan 09 '19 at 19:45
  • Possible duplicate of [Can I find events bound on an element with jQuery?](https://stackoverflow.com/questions/2008592/can-i-find-events-bound-on-an-element-with-jquery) – freedomn-m Jan 09 '19 at 19:45

0 Answers0