English 中文(简体)
为什么MDN建议在某些情况下使用定时?
原标题:Why MDN suggest use setTimeout in some situation?

MDN 说明

Ensure that execution duration is shorter than interval frequency If there is a possibility that your logic could take longer to execute than the interval time, it is recommended that you recursively call a named function using setTimeout(). For example, if using setInterval() to poll a remote server every 5 seconds, network latency, an unresponsive server, and a host of other issues could prevent the request from completing in its allotted time. As such, you may find yourself with queued up XHR requests that won t necessarily return in order.

因此,一 4个样本

let start;
let count = 1;
setInterval(() => {
  let b = 0;
  for (let i = 0; i < 100000000; i++) {
    b += i;
  }
  if (!start) start = performance.now();
  else console.log((performance.now() - start) / count++);
}, 200);

let start;
let count = 1;
setInterval(() => {
  let b = 0;
  for (let i = 0; i < 100000000; i++) {
    b += i;
  }
  if (!start) start = performance.now();
  else console.log((performance.now() - start) / count++,b);
}, 200);

function mySetinterval(fn, timeout, ...args) {
  let timer;
  function loop() {
    timer = setTimeout(() => {
      fn(...args);
      loop();
    }, timeout);
  }
  loop();
  return function () {
    clearTimeout(timer);
    timer = null;
  };
}


let start;
let count = 1;
mySetinterval(() => {
  let b = 0;
  for (let i = 0; i < 100000000; i++) {
    b += i;
  }

  if (!start) start = performance.now();
  else console.log((performance.now() - start) / count++);
}, 200);

function mySetinterval(fn, timeout, ...args) {
  let timer;
  function loop() {
    timer = setTimeout(() => {
      fn(...args);
      loop();
    }, timeout);
  }
  loop();
  return function () {
    clearTimeout(timer);
    timer = null;
  };
}
let start;
let count = 1;
mySetinterval(() => {
  let b = 0;
  for (let i = 0; i < 100000000; i++) {
    b += i;
  }

  if (!start) start = performance.now();
  else console.log((performance.now() - start) / count++,b);
}, 200);

我认为, 平均实际执行所有法典,因此下游动物在200米后会发生。

和 及时性将执行所有法典,而时段的缺口将可能与编码脱钩,即通过pu法进行估算。

是否正确? 而我不理解为什么国际发展部说XHR会影响Interval,你会举一个例子吗?

问题回答

settimeout for XHR request

以上答复解释了为什么国家排雷局在间隔期使用XHR?

And I think setTimeout will perform better will in this siuation. blocking loop code will cause task has many macro task setInterval

let startTime = performance.now();
setInterval(() => {
  console.log(performance.now() - startTime);
  const count = Math.floor(10000000000 * Math.random());
  for (let i = 0; i < count; i++) {}
  startTime = performance.now();
}, 1000);

造成每一次休息时间间隔





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签