I have a script that look at a phone call queue every second and display updates to the user. "for now my code writes messages to the console for testing purposes."
Since my code runs every 1 second, when a user opens multiple table, the same function is called 1 x "the total tabs" per second. If I have 5 tabs open, that is 5 requests per second, which could give the user false info or cause issues.
Is there a way for me to run the setInterval()
function only on the active tab? My code will have to be executed on every page of the application to make sure the user is notify if there is an inbound call or something he or she needs to be aware of.
Here is the code that is executed every second.
<script>
$(function(){
function isset(a, b){
if(typeof a !== "undefined" && a){
return a
}
return b;
}
setInterval(function() {
$.getJSON("showMEssages.php", {method: "getMessages"}, function(data){
if(typeof data.calls === 'undefined' || data.calls.length == 0 || !data.calls){
console.log('No Messages');
return;
}
$.each(data.calls, function(i, item){
$.each(item, function(z, c){
var interactionId = isset(c.interactionId, 0);
var Eic_CallDirection = isset(c.Eic_CallDirection, '');
var Eic_State = isset(c.Eic_State, '');
var AccoRDI_mid = isset(c.AccoRDI_mid, '');
var Eic_RemoteAddress = isset(c.Eic_RemoteAddress, '');
if(Eic_State == '' || Eic_CallDirection == ''){
return;
}
//incoming call that is not answered
if( Eic_CallDirection == 'I' && Eic_State == 'A'){
console.log('Incomming Call From ' + Eic_RemoteAddress + ' MID: ' + AccoRDI_mid );
return;
}
//on hold
if( Eic_State == 'H'){
console.log('Phone number ' + Eic_RemoteAddress + ' is on hold' );
return;
}
//voicemail
if( Eic_State == 'M'){
console.log('Phone number ' + Eic_RemoteAddress + ' is on leaving a voicemail' );
return;
}
//connected call
if(Eic_State == 'C'){
console.log('Live Call With ' + Eic_RemoteAddress );
return;
}
//Dialling call
if(Eic_State == 'O'){
console.log('Dialling ' + Eic_RemoteAddress );
return;
}
//Dialling call
if(Eic_State == 'R'){
console.log('Outbound call is rining and waiting for answer ' );
return;
}
//Call Disconnected
if(Eic_State == 'I' || Eic_State == 'E' ){
console.log('Hungup with ' + Eic_RemoteAddress );
return;
}
});
});
});
}, 1000);
});
</script>
any alternative solution to this?