I have a jQuery dialog that appears and loads an external page. In that page i am running a setInterval() function that queries my server continuously every 1 second (AJAX). The problem is that when i close the dialog, the setInterval doesn’t stop running.
here is the code for the dialog:
var theUrl = 'someUrl';
var popUp = document.createElement('div');
$(popUp).dialog({
width: 400,
height: 270,
title: "Some Title",
autoOpen: true,
resizable:false,
close: function(ev, ui) {
$(this).dialog('destroy');
},
modal: true,
open: function() {
$(this).load(theUrl);
}
});
I tried calling $(this).dialog('destroy') and $(this).remove() and document.body.removeChild(popUp) on close. nothing worked. is there anyway to ‘unload’ the loaded page?
setIntervalreturns a handler that you can pass toclearIntervalto stop the function from running. Here’s a basic example of how it works.For your example you’d want to call
clearIntervalin theclosemethod of theui.dialog.Docs:
setInterval– https://developer.mozilla.org/en/window.setIntervalclearInterval– https://developer.mozilla.org/en/DOM/window.clearIntervalEdit
You will not be able to call
clearIntervalwithout the stored handler fromsetInterval, therefore if the call tosetIntervalis in another script the only way you’re going to capture the handler is to overridewindow.setIntervalitself.Note that the code to override
window.setIntervalmust come before including the<script>tag to bring in the external file. Also this approach will clear all interval functions wheneverclearIntervalis called, therefore this is not ideal, but it’s the only way you’re going to accomplish this.