“从第一原理"模拟JavaScript的setTimeout()方法 [英] Simulating JavaScript's setTimeout() method “from first principles”

查看:103
本文介绍了“从第一原理"模拟JavaScript的setTimeout()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Firefox的 Scratchpad .关于此特定JavaScript方法,似乎有许多Firefox错误-它是双胞胎setInterval().在Firefox v.56和最新的Waterfox(均在"Quantum"之前)中,这些JavaScript方法似乎根本不起作用.相比之下,在Firefox"Quantum"版本中,它确实可以工作……也就是说,相同的代码在FF"Quantum"中也可以工作,而 不能在前一个版本中正常工作." Quantum"版本的Firefox.是的,我尝试了各种变体.

这种情况是,我坚持使用Quantum之前的版本进行此练习,因此我需要找到一种方法从最初的原理构建"此setTimeout()方法. /p>

一种想法是在继续执行进一步的代码/脚本之前,获取当前日期/时间,然后循环检查一下是否已经过了10秒(或5分钟).

任何想法如何使用JavaScript高效地模拟setTimeout()资源,但 没有 setTimeout()setInterval()吗?


---编辑---

错误警报...也可能会导致setInterval()在较旧的浏览器中运行;我不好!

(这意味着提出问题的原因已消失,但这样的问题可能仍然存在...)


--- 2nd编辑---

setTimeout()在这里工作:

setTimeout(function(){ alert("Hello"); }, 3000);

它不起作用/在这里似乎被忽略了:

i=0;
while(i < 100)
{
 // window.open("https://www.cnn.com","_self");
 // window.open("https://www.bbc.com","_self");
   // setTimeout(function(){ alert("Hello"); }, 3000);
   setTimeout(function(){ window.open("https://www.bbc.com","_self") }, 3000);
  setTimeout(function(){ window.open("https://www.cnn.com","_self") }, 3000);
  alert(i);
  i++;
}
  // security mgr vetoed ???

  • 为什么?

解决方案

如果您真的想在不阻止浏览器的情况下模拟setTimeout函数,则可以尝试使用requestAnimationFrame函数获取一些信息延迟.它应该可以在Firefox 14.0+中使用.像这样的东西:

function mySetTimeout (callback, timeout) {
  var startTime = performance.now();

  function mySetTimeoutRec () {
    requestAnimationFrame(function () {
      // This way this function will be called
      // asynchronously around 60 times per second
      // (or less frequently on the inactive tabs).
      // We can check that enough time has passed
      // and call the user callback or this function again.
      var currentTime = performance.now();
      var elapsedTime = currentTime - startTime;

      if (elapsedTime < timeout) {
        mySetTimeoutRec();
      } else {
        callback();
      }
    });
  }

  mySetTimeoutRec();
}

它可以像setTimeout一样使用:

mySetTimeout(function () { console.log('Howdy.'); }, 10 * 1000);

无论如何,在大多数情况下,您都不应尝试执行类似的操作.如果您似乎对setTimeout有问题,则可能是其他错误所在.

I'm trying to run setTimeout() on a website via Firefox's Scratchpad. It seems that there were various Firefox bugs regarding this particular JavaScript method - and it's twin, setInterval(). In Firefox v.56 and the latest Waterfox (both pre-"Quantum"), these JavaScript methods do not seem to work at all. By contrast, in the Firefox "Quantum" versions, it does seem to work... That is, the very same code works in FF "Quantum", which does not work in the immediate pre-"Quantum" versions of Firefox. And yes, I've tried all sorts of variations.

Circumstance has it that I'm stuck with the pre-Quantum version(s) of Firefox for this exercise and I need to find a way to "build" this setTimeout() method from first principles, as it were.

One thought would be to take the current date/time and then loop through a check to see if, say, 10 seconds (or 5 minutes) have passed, before continuing with further code/script execution.

Any ideas how to simulate setTimeout() resource-efficiently with JavaScript but without setTimeout() or setInterval() ?


---EDIT---

False alarm... could get setInterval() to work in the older browsers too; my bad!

(this means the reason for asking the question is gone, but the question as such may remain...)


---2nd EDIT---

Whereas setTimeout() works here:

setTimeout(function(){ alert("Hello"); }, 3000);

it does not work / seems to be ignored here:

i=0;
while(i < 100)
{
 // window.open("https://www.cnn.com","_self");
 // window.open("https://www.bbc.com","_self");
   // setTimeout(function(){ alert("Hello"); }, 3000);
   setTimeout(function(){ window.open("https://www.bbc.com","_self") }, 3000);
  setTimeout(function(){ window.open("https://www.cnn.com","_self") }, 3000);
  alert(i);
  i++;
}
  // security mgr vetoed ???

  • Why?

解决方案

If you really-really want to simulate the setTimeout function without blocking the browser and so on, you can try use the requestAnimationFrame function to get some delay. It should work in the Firefox 14.0+. Something like that:

function mySetTimeout (callback, timeout) {
  var startTime = performance.now();

  function mySetTimeoutRec () {
    requestAnimationFrame(function () {
      // This way this function will be called
      // asynchronously around 60 times per second
      // (or less frequently on the inactive tabs).
      // We can check that enough time has passed
      // and call the user callback or this function again.
      var currentTime = performance.now();
      var elapsedTime = currentTime - startTime;

      if (elapsedTime < timeout) {
        mySetTimeoutRec();
      } else {
        callback();
      }
    });
  }

  mySetTimeoutRec();
}

It can be used just like setTimeout:

mySetTimeout(function () { console.log('Howdy.'); }, 10 * 1000);

Anyway you should not try to do something like that in the most cases. If it seems that you are have problems with setTimeout, that's probably something else is wrong.

这篇关于“从第一原理"模拟JavaScript的setTimeout()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆