javascript - How to prevent 2 setInterval() function to run at the same time -
i have script @ phone call queue every second , display updates user. "for code writes messages console testing purposes."
since code runs every 1 second, when user opens multiple table, same function called 1 x "the total tabs" per second. if have 5 tabs open, 5 requests per second, give user false info or cause issues.
is there way me run setinterval()
function on active tab? code have executed on every page of application make sure user notify if there inbound call or or needs aware of.
here code executed every second.
<script> $(function(){ function isset(a, b){ if(typeof !== "undefined" && a){ return } 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 not answered if( eic_calldirection == 'i' && eic_state == 'a'){ console.log('incomming call ' + eic_remoteaddress + ' mid: ' + accordi_mid ); return; } //on hold if( eic_state == 'h'){ console.log('phone number ' + eic_remoteaddress + ' on hold' ); return; } //voicemail if( eic_state == 'm'){ console.log('phone number ' + eic_remoteaddress + ' on leaving voicemail' ); return; } //connected call if(eic_state == 'c'){ console.log('live call ' + 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 rining , waiting answer ' ); return; } //call disconnected if(eic_state == 'i' || eic_state == 'e' ){ console.log('hungup ' + eic_remoteaddress ); return; } }); }); }); }, 1000); }); </script>
any alternative solution this?
you implement mechanism synchronise works between different tabs. main idea let tabs communicate each other
for example, once user opens tabs, broadcast message channel saying "i looking active one". active tab (listening on same channel) respond: "i alive", , new opened tab not start interval function. otherwise start interval , mark active. in practice, might need other things eliminate deadlock (using watch dog technique example)
there other ways general idea let tabs communicate each other
some libraries might implement it
Comments
Post a Comment