简体   繁体   English

JavaScript 的广播频道是否仅限于每秒接收一条消息?

[英]Is JavaScript's broadcast channel limited to one received message per second?

I may have answered this question for myself but I need to sanity check...我可能已经为自己回答了这个问题,但我需要进行理智检查......

I have one tab broadcasting to another over the broadcast channel every 500 ms.我每 500 毫秒通过广播频道向另一个选项卡广播。 The second tab is set to receive broadcasts and print them in the console log.第二个选项卡设置为接收广播并在控制台日志中打印它们。

Sending tab:发送标签:

const crossTraffic = new BroadcastChannel('crossTraffic');

const queueInterval = setInterval(function(){
   crossTraffic.postMessage({action: 'keepalive', cTime: Date.now()});
   console.log(`cTime sent ${Date.now()}`);
    }
},500);

Receiving tab:接收标签:

const crossTraffic = new BroadcastChannel('crossTraffic');

crossTraffic.onmessage = function(e){
  if (e.data.action === 'keepalive'){
      console.log(`ctime received ${e.data.cTime}`);
    }
  }

I've observed the following: no matter what I set the queueInterval time for, the receiving tab will never receive a broadcast more than once every 1,000 ms.我观察到以下情况:无论我将 queueInterval 时间设置为什么,接收选项卡都不会每 1,000 毫秒接收一次以上的广播。

Is this normal behavior for the broadcast channel or is it possible to receive messages at a smaller interval than 1,000 ms?这是广播频道的正常行为还是可以以小于 1,000 毫秒的间隔接收消息?

This is a different issue than I thought...这是一个与我想象的不同的问题......

Turns out that tabs that aren't the focus (at least in Chrome) are limited to one update per second.事实证明,不是焦点的标签(至少在 Chrome 中)仅限于每秒更新一次。 If I open separate windows and have one broadcast to the other, each message arrives at whatever interval I select (tested at 100ms).如果我打开单独的窗口并将一个广播发送到另一个窗口,则每条消息都会以我选择的任何时间间隔到达(以 100 毫秒进行测试)。

Here's the modified code I used for testing on the sending window/tab:这是我用于在发送窗口/选项卡上进行测试的修改后的代码:

const crossTraffic = new BroadcastChannel('crossTraffic');

var queueInterval;
var count = 0;

  queueInterval = setInterval(function(){
       count++
        if (count >= 100){
            clearInterval(queueInterval);
          }
       crossTraffic.postMessage({action: 'keepalive', cTime: Date.now(), count: count});
       console.log(`cTime sent ${Date.now()}`);
        }
    },100);

And the modified receiver:和修改后的接收器:

const crossTraffic = new BroadcastChannel('crossTraffic');

crossTraffic.onmessage = function(e){
  if (e.data.action === 'keepalive'){
      console.log(`ctime received ${e.data.cTime}, #${e.data.count}`);
    }
  }

You'll observe that if both sender and receiver are tabs in the same window, the receiver will be logging them only once per second, yet all 100 messages will be sent and received.您会观察到,如果发送方和接收方都是同一窗口中的选项卡,则接收方每秒只会记录一次,但会发送和接收所有 100 条消息。

Then, pull one of the tabs out into its own window and place them side by side, watching their logs: the message will send and receive at a 100 ms interval.然后,将其中一个选项卡拉出自己的窗口并将它们并排放置,观察它们的日志:消息将以 100 毫秒的间隔发送和接收。

Overall, excellent news as I was afraid the broadcast channel had some strict limitations I was unaware of.总的来说,这是个好消息,因为我担心广播频道有一些我不知道的严格限制。

Now to determine how to force background tabs to update more than once per second...现在确定如何强制后台选项卡每秒更新一次以上......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在javascript中每秒显示一行 - show one line per second in javascript Javascript 每秒计算 - Javascript per second calculation 输入范围限制为每个表格一个 - Input Range Limited to one per form 递归 function 调用,setTimeout 限制为每秒约 200 次调用,为什么? - Recursive function call with setTimeout limited to ~200 calls per second, why? 向客户端广播Javascript Websocket服务器消息 - Javascript Websocket server message broadcast to clients Python Web Scraper-由页面JavaScript定义的每页结果有限 - Python Web Scraper - limited results per page defined by page JavaScript 为什么这个 javascript function 每秒执行的次数比预期的多 - Why is this javascript function executing more times per second than it's supposed to AngularJS(1.2.25)$ scope。$ on('message')未收到$ rootScope。$ broadcast('message',data) - AngularJS (1.2.25) $rootScope.$broadcast('message',data) not being received by $scope.$on('message') 出现一秒钟的消息,然后消失Javascript - Message that appears for a second and then dissapears Javascript JavaScript Discord如何在服务器上和特定通道上的通道上检测到消息? - JavaScript Discord How can the message be detected on a server and on the channel, on a specific channel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM