I need to call an Ajax script to reload data from a child window. I then need to close said child window. I have been having issues with the Ajax script halting when the window closes, so I tried using this:
MakeParentRequest('get_job.php?tab=journals&id=<?php echo $cur_job->job_id; ?>', 'jobscontainer', 'yes', 'yes');
setTimeout("window.close()", "1000");
MakeParentRequest() loads the script开发者_如何学运维 'get_job.php' in element with ID 'jobscontainer'. This code still encounters the same issue if 'get_job.php' takes longer to load than the timeout duration. Suggestions?
A better way to handle it would be to define a callback function that's called when your Ajax call is complete.
Here's a simple (jQuery-centric) example:
function ajaxComplete() {
window.close();
}
function MakeParentRequest() {
$.post('get_job.php', {tab: 'journals'}, function( data, txtStatus, jqXHR) {
ajaxComplete();
});
}
Then it doesn't matter how long the ajax call takes, you won't close the window until it's done. Plus it lets you better handle the case where your ajax call fails and you need to do something else.
You could even pass the callback as an argument to MakeParentRequest()
function MakeParentRequest(callback) {
$.post('get_job.php', {tab: 'journals'}, function( data, txtStatus, jqXHR) {
if(typeof callback == 'function') {
callback();
}
});
}
MakeParentRequest(ajaxComplete);
精彩评论