如果浏览器正忙,请取 [英] Get if browser is busy

查看:118
本文介绍了如果浏览器正忙,请取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法来获取浏览器当前是否正忙于JavaScript。我正在考虑让Firefox扩展插入一个布尔值,如果当前页面正在加载某些东西(通过ajax或者只是普通的页面加载),或者与Greasemonkey脚本相同,或者通过一些JavaScript API(这会是最好的解决方案,但是从我所能看到的情况来看,没有任何一种存在)。

我想知道做这件事的最好方法是什么。我一直在寻找Firefox的Addon / Greasemonkey教程做这样的事情,找不到任何东西。有没有人有任何提示或资源,他们可以指向我或更好的解决方案来解决这个问题?/ b>

谢谢

编辑:通过忙,我大多只需要知道浏览器是否正在发送或从服务器接收数据。

解决方案

我想我最终会做的。这个解决方案就像Alex提供给Jquery事件的解决方案一样,只是它可以处理任何使用XMLHttpRequest(包括Jquery)的东西:

  var runningAjaxCount = 0; 

var oldSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(){
oldOnReady = this.onreadystatechange;
this.onreadystatechange = function(){
oldOnReady.call(this);
if(this.readyState == XMLHttpRequest.DONE){
ajaxStopped();
}
}
ajaxStarted();
oldSend.apply(this,arguments);
}

函数ajaxStarted(){
runningAjaxCount ++;
}

函数ajaxStopped(){
runningAjaxCount--;
}

函数isCallingAjax(){
return runningAjaxCount> 0;


函数isBrowserBusy(){
return document.readyState!=complete&& isCallingAjax();
}


I'm trying to find a way to get if the browser is currently busy from JavaScript. I'm looking at making a Firefox extension to inject a Boolean value or something if the current page is loading something (either through ajax or just normal page loads), or the same with a Greasemonkey script, or through some JavaScript API (this would be the best solution, but from what I can see, nothing of the sort exists).

I was wondering what the best way to do this would be. I've been looking for Firefox Addon / Greasemonkey tutorials for making something like this and can't find anything. Does anyone have any tips or resources they could point me towards or better solutions for solving this?

Thanks

Edit: and by busy, I mostly just need to know if the browser is sending or receiving data from a server.

解决方案

Here's what I think I'll end up doing. This solution is like the one Alex suggested with the Jquery events, except that it works with anything that uses the XMLHttpRequest (Including Jquery):

var runningAjaxCount = 0;

var oldSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function() {
    oldOnReady = this.onreadystatechange;
    this.onreadystatechange = function() {
        oldOnReady.call(this);
        if(this.readyState == XMLHttpRequest.DONE) {
            ajaxStopped();
        }
    }
    ajaxStarted();
    oldSend.apply(this, arguments);
}

function ajaxStarted() {
    runningAjaxCount++;
}

function ajaxStopped() {
    runningAjaxCount--;
}

function isCallingAjax() {
    return runningAjaxCount > 0;
}

function isBrowserBusy() {
    return document.readyState != "complete" && isCallingAjax();
}

这篇关于如果浏览器正忙,请取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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