JavaScript - 是否可以查看所有当前计划的超时? [英] JavaScript - Is it possible to view all currently scheduled timeouts?

查看:95
本文介绍了JavaScript - 是否可以查看所有当前计划的超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想知道,在JavaScript中是否有任何可行的方法来查看有关您未明确了解的预定超时和间隔的信息(我知道 setTimeout setInterval 返回一个句柄,可以用来引用计划的实例,但是说这个因某种原因而不可用)?例如,有没有办法使用像Chrome的JavaScript控制台这样的工具来确定任意页面上当前处于活动状态的超时,什么时候它们将会触发,以及在它们触发时将执行哪些代码?更具体地说,一个页面刚刚执行了以下JavaScript:

So I was wondering, is there any feasible way in JavaScript to view information about scheduled timeouts and intervals that you don't explicitly know about (I know setTimeout and setInterval return a handle that can be used to refer to the scheduled instance, but say that this is unavailable for one reason or another)? For instance, is there a way to use a tool like Chrome's JavaScript console to determine what timeouts are currently active on an arbitrary page, when they will fire, and what code will be executed when they fire? More specifically, say a page has just executed the following JavaScript:

setTimeout("alert('test');", 30000);

此时我是否可以执行一些代码,告诉我浏览器将执行 alert('test'); 从现在开始30秒?

Is there some code I can execute at this point that will tell me that the browser will execute alert('test'); 30 seconds from now?

理论上似乎应该有某种方法来获得这个信息,因为如果你知道在哪里查看,JavaScript中的所有内容都会被公开作为一个公共可访问的属性,但我不记得曾经自己做过这样做或者看到别人做过的事实。

It seems like there theoretically should be some way to get this information since pretty much everything in JavaScript is exposed as a publicly accessible property if you know where to look, but I can't recall an instance of ever doing this myself or seeing it done by someone else.

推荐答案

如何简单地重写setTimeout函数以排序注入自定义日志记录功能?

how about simply rewriting the setTimeout function to sort of inject custom logging functionality?

喜欢

var oldTimeout = setTimeout;
window.setTimeout = function(callback, timeout) {
  console.log("timeout started");
  return oldTimeout(function() {
    console.log('timeout finished');
    callback();
  }, timeout);
}

可能有用吗?

这篇关于JavaScript - 是否可以查看所有当前计划的超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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