检测是否有任何JavaScript函数正在运行 [英] Detect if any JavaScript function is running

查看:107
本文介绍了检测是否有任何JavaScript函数正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能听起来很奇怪,但我需要知道页面中是否有任何活动/正在运行的JavaScript。



我处于这种情况必须在页面上的所有内容都呈现完毕并且所有其他脚本完成后运行我的javascript / jquery代码。



是否可以检测到这个?



编辑:



谢谢大家的回答。不幸的是,我无法找到解决方案,因为我无法完全控制页面上发生的事情。

甚至,我能够将我的javascript放在页面的最后,我认为它不会再解决。原因是当页面呈现一个函数被触发时,它会调用其他函数并且他们调用其他函数等等。结果,一些数据是不正确的,这就是为什么我需要运行我的代码来纠正它。



我用2秒的setTimeout来确保我的代码将会最后执行,但这是丑陋的...

所以,谢谢大家,但这是系统的问题,而不是js。

解决方案

Web浏览器上的JavaScript是单线程的(禁止使用

为了确保脚本在页面上的所有其他JavaScript已经下载并评估以及所有渲染发生后都会发生,有一些建议:



所以, 脚本标记:

 < 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 and async 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, or attachEvent on older IE versions).
  • In the load event, schedule your code to run after a setTimeout 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屋!

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