0

Every 10 seconds, it calls this ajax to reload the number of received mails.
Sometime, I open my application at over 5 tabs at once.
Obviously each tab try to call this ajax so my PC gets so heated :(

I want to run this ajax only in the tab which is in active. So rest of 4 tabs shouldn't call the ajax unless the tab is active.

How can I implement that condition to this?

refresh_mail_count.js

jQuery(document).ready(function () {
    refreshMail();
});


function refreshMail() {
  $.ajax({
    url: "/messages/refresh_mail",
    type: "GET",
    dataType: "script",
  });
}

refresh_mail.js.erb

$('#message_count').html("<%= j(render(:partial => 'layouts/message_received_count', :object => @message_count)) %>");
setTimeout(refreshMail,10000);
cat
  • 749
  • 10
  • 22
  • 1
    look at http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active to check if the tab is active. – jvnill Feb 05 '13 at 14:32
  • @jvnill Thanks. Ouchy:( because it all depends on what browser and what version I'm using. Isn't there any possible way to detect if the same Ajax function is already running in different tab? Then if so, is there any technique to stop ajax call? – cat Feb 05 '13 at 14:41
  • nope i don't think that is possible. you can try to use `localStorage` but I'm not sure how. I wish I could help you. – jvnill Feb 05 '13 at 14:44
  • @jvnill Thanks though:) Let's wait someone will come to solve this:) – cat Feb 05 '13 at 14:47

0 Answers0