检测是否有任何JavaScript函数正在运行 [英] Detect if any JavaScript function is running
问题描述
我知道这可能听起来很奇怪,但我需要知道页面中是否有任何活动/正在运行的JavaScript。
我处于这种情况必须在页面上的所有内容都呈现完毕并且所有其他脚本完成后运行我的javascript / jquery代码。
是否可以检测到这个?
编辑:
谢谢大家的回答。不幸的是,我无法找到解决方案,因为我无法完全控制页面上发生的事情。
甚至,我能够将我的javascript放在页面的最后,我认为它不会再解决。原因是当页面呈现一个函数被触发时,它会调用其他函数并且他们调用其他函数等等。结果,一些数据是不正确的,这就是为什么我需要运行我的代码来纠正它。
我用2秒的setTimeout来确保我的代码将会最后执行,但这是丑陋的...
所以,谢谢大家,但这是系统的问题,而不是js。
Web浏览器上的JavaScript是单线程的(禁止使用 为了确保脚本在页面上的所有其他JavaScript已经下载并评估以及所有渲染发生后都会发生,有一些建议: 所以, ...和 这并不意味着其他代码不会计划自己稍后运行(通过
defer
an d async
属性(它们会被不支持它们的浏览器忽略,但目标是让您成为 last
窗口
load $ c $在具有标准支持的浏览器上使用DOM2风格的连接(例如,
addEventListener
,或者在旧版本的IE版本上使用 attachEvent
)
load
事件中,计划您的代码在 setTimeout $ c $之后运行延迟时间为0毫秒(它不会真的为零,它会稍微长一点)。
脚本
标记:
< script async defer src =yourfile。 JS>< /脚本>
yourfile.js
:
(function(){
if(window.addEventListener){
window.addEventListener(load ,loadHandler,false);
}
else if(window.attachEvent){
window.attachEvent(onload,loadHandler);
}
else {
window.onload = loadHandler; //或者你可能想关闭它,只是不支持真正的旧浏览器
}
函数loadHandler(){
setTimeout (doMyStuff,0);
}
函数doMyStuff(){
//你的东西在原始标记中的所有图像都保证为
//被加载(或失败)的`load`事件,并且你知道
//其他的`load`事件处理器现在已经被触发,因为
//我们从我们的`load`处理程序
}
})();
setTimeout
,例如,就像我们上面所做的那样,但有一个更长的超时时间),但是。
(*有些情况下线程可以暂停在一个地方,然后允许其他代码在另一个地方运行[例如,当显示 alert
消息时ajax调用完成时,即使另一个函数正在等待,某些浏览器也会触发ajax处理程序> alert
被解雇],但它们是边缘案例,并且一次只有一件事情正在积极完成。)
I know it may sound very strange, but I need to know if there is any active/running javascript in the page.
I am in situation in which I have to run my javascript/jquery code after everything on the page is rendered and all other scripts have finished.
Is it possible to detect this?
EDIT:
Thank you all for the answers. Unfortunately, I was not able to find a solution, because I have no full control of what is going on the page.
Even, I was able to put my javascript in the end of the page, I think it will not be solution again. The reason is that when the page is rendering a function is triggered, it calls other functions and they calls other and so on. As a result, some of the data is incorrect and that's why i need to run my code to correct it.
I use setTimeout with 2 seconds to ensure that my code will be executed last, but this is ugly...
So, thank you all, but this is more problem with the system, not the js.
JavaScript on web browsers is single-threaded (barring the use of web workers), so if your JavaScript code is running, by definition no other JavaScript code is running.*
To try to ensure that your script occurs after all other JavaScript on the page has been downloaded and evaluated and after all rendering has occurred, some suggestions:
- Put the script tag for your code at the very end of the file.
- Use the
defer
andasync
attributes on the tag (they'll be ignored by browsers that don't support them, but the goal is to make yours the last as much as we can). - Hook the
window
load
event via a DOM2 style hookup (e.g.,addEventListener
on browsers with standards support, orattachEvent
on older IE versions). - In the
load
event, schedule your code to run after asetTimeout
with a delay of 0ms (it won't really be zero, it'll be slightly longer).
So, the script
tag:
<script async defer src="yourfile.js"></script>
...and yourfile.js
:
(function() {
if (window.addEventListener) {
window.addEventListener("load", loadHandler, false);
}
else if (window.attachEvent) {
window.attachEvent("onload", loadHandler);
}
else {
window.onload = loadHandler; // Or you may want to leave this off and just not support REALLY old browsers
}
function loadHandler() {
setTimeout(doMyStuff, 0);
}
function doMyStuff() {
// Your stuff here. All images in the original markup are guaranteed
// to have been loaded (or failed) by the `load` event, and you know
// that other handlers for the `load` event have now been fired since
// we yielded back from our `load` handler
}
})();
That doesn't mean that other code won't have scheduled itself to run later (via setTimeout
, for instance, just like we did above but with a longer timeout), though.
So there are some things you can do to try to be last, but I don't believe there's any way to actually guarantee it without having full control of the page and the scripts running on it (I take it from the question that you don't).
(* There are some edge cases where the thread can be suspended in one place and then allow other code to run in another place [for instance, when an ajax call completes while an alert
message is being shown, some browsers fire the ajax handler even though another function is waiting on the alert
to be dismissed], but they're edge cases and there's still only one thing actively being done at a time.)
这篇关于检测是否有任何JavaScript函数正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!