Panel: custom field -> remove listeners

In the panel with a custom field that uses javascript in the jQuery.fn.fieldname = function() { } way, how do I clean up after the page changes? Removing listeners etc… Is there an event somewhere that I can subscribe to?

For the time being, I am resorting to polling if the container element is still attached to the dom. Would be great if there was another way though.

function checkDestroy() {
  if (!document.contains(container)) {
    // remove event listeners / intervals here
var intervalId = window.setInterval(checkDestroy, 2000);

What does the interval do that you clear? Maybe it can be expressed using an event in the first place.

I have an audio element with an mp3 stream as its src. i need to clear the src when the editor moves to another section, otherwise it just keeps loading in the background.