I have a JavaScript loop that checks a database table for new messages as they come in. This works off an AJAX request, and is working fine. My problem arises whenever I try to navigate to another page (Only in Chrome). The page hangs, almost as though the page is waiting for the script to complete before redirecting/reloading. I require a way that I can stop the script running when the page is unloaded or reloaded. I have tried using jQuery's onbeforeunload function and binding it to $(window), but the problem is still apparent. I'm not sure where/what to try. After a google search, all I can find are methods to stop the page redirecting altogether unless "Yes" is clicked in a confirm window. This is not what I need.
Just to clarify, I need a method that will do the following:
- User clicks to navigate away/reload the page
- JavaScript identifies this and stops the script dead in it's tracks, regardless of progress. (I'm not fussed whether the script is about to complete or it has just started)
- Once the script has been stopped, then redirect to the desired page.
It seems like this is overly simple to do, but i can't figure it out.
Any help will be massively appreciated!
Code:
// Document Ready that will initiate the loop.
$(document).ready(function() {
// Notification System Start
var onLoadMsgs = document.getElementById("currentNotifs").value;
getRedMsgUpdates(onLoadMsgs, true);
// Notification System End
});
// AJAX Function that will get the # of msgs
function getRedMsgUpdates() {
var pageDetect = document.URL.indexOf("job.php");
var self = this;
var results = {};
var updateUrl = "/includes/record_count.php";
$.ajax({
method: 'GET',
cache: false,
url: updateUrl,
datatype: 'text',
data: {
},
success:function(redResponse){
if(redResponse == 0) {
var redResponse = " " + redResponse;
} else if (redResponse >= 1 && redResponse <= 9) {
var redResponse = " " + redResponse;
} else if (redResponse >= 10 && redResponse <= 99) {
var redResponse = redResponse;
} else if (redResponse >= 100) {
var redResponse = "99+";
} else {
var redResponse = "ERR";
}
document.getElementById('secoNotifsNum').innerHTML = redResponse;
if(redResponse == " " + 1 && pageDetect == "41") {
document.getElementById('redBarText').innerHTML = '<a target="_blank" href="/includes/Messages.php">' + redResponse + ' Emergency Message Outstanding</a>';
} else if(redResponse == " " + 1) {
document.getElementById('redBarText').innerHTML = '<a href="/includes/Messages.php">' + redResponse + ' Emergency Message Outstanding</a>';
} else if(redResponse != " " + 1 && pageDetect == "41") {
document.getElementById('redBarText').innerHTML = '<a target="_blank" href="/includes/Messages.php">' + redResponse + ' Emergency Messages Outstanding</a>';
} else if(redResponse != " " + 1) {
document.getElementById('redBarText').innerHTML = '<a target="_blank" href="/includes/Messages.php">' + redResponse + ' Emergency Messages Outstanding</a>';
}
flashRedBackground(redResponse);
}
});
timeout1 = setTimeout(function() {getRedMsgUpdates();}, 2000);
}
// Function that will make the notification Bar/Circle flash
function flashRedBackground(msgs) {
var audio = new Audio('/js/audio/siren1.wav');
if(msgs != " " + 0) {
// Flash Background
$("#notificationRedFull").show();
$("#notificationRedFull").css("opacity", "1");
$("#notificationRedFull").fadeOut(1000).fadeIn(1000).fadeOut(1000).fadeIn(1000).fadeOut(1000).fadeIn(1000);
$("#notificationBarRed").css("opacity", "1");
$("#notificationBarRed").fadeOut(1000).fadeIn(1000).fadeOut(1000).fadeIn(1000).fadeOut(1000).fadeIn(1000);
audio.play();
} else if(msgs == " " + 0) {
// Ensure Notification Not Shown
$("#notificationRedFull").hide();
$("#notificationRedFull").css("opacity", "0");
$("#notificationBarRed").css("display", "none");
}
}