簡體   English   中英

如何限制跨多個瀏覽器選項卡運行的通用腳本?

[英]How to restrict common script running across multiple browser tabs?

我制作了一個用於檢查未讀通知的腳本。 它必須在所有屏幕上工作,所以它已經寫在我項目的 header 中,並且運行良好。 由於它是用 header 編寫的,因此如果我打開多個選項卡,我想避免它,這意味着即使一次打開多個選項卡,也應僅在一個選項卡上檢查此過程。

window.onload=function(){
  fnCheckOrdStaNoti();

  setInterval(fnCheckOrdStaNoti, 15000);
}

function fnCheckOrdStaNoti(){
  $.ajax({
    url: 'notification.php',
    type: 'post',
    dataType: 'html',
    crossDomain: true,
    
    success: function(hdata) {
     // hdata has some information regarding notification
     // based on result i trigger another ajax function.
     // here i have to check how many no of browser tab has been opened with respect to my project. and have to be triggered another ajax function to the first tab only, rest should be restricted.
    },
    error: function(xhr, ajaxOptions, thrownError) {
      console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    }
  });
}

最后,我找到了解決我的問題的方法。 我從這個url 中提到了@Reza Amya 的建議。 我已經根據我的要求對其進行了更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM