English 中文(简体)
a. 多个jax要求,没有上页
原标题:multiple ajax requests without page refresh

i have an app (struts2+sprin+glassfish3.1.2) that sends an ajax (jquery) requests every second to the server. If certain conditions are met my js redirects me to other page, otherwise i do nothing.
It seems that after a long period (couple of hours without any redirect) the browser become unresponsive or it take a very long to time perform a request.
I mention that if when this happens i close the browser from the X and reopen it everything is ok. I did not manage to replicate this problem in my test environment, but it seems the i have this problem in production :( .

any ideea ?

i use Jquery (struts2-jquery-plugin3.3.0) for ajax calls,i will attach some of the code:
this method is accessed every 1 second :

function checkedForAnsweredCalls(){

    if (ajaxFinished == 1){

        ajaxFinished = 0;

        $.getJSON( redirectToTicket.html ,
                function(data) {

                    ajaxFinished = 1;
                    if (data != null){                      
                        window.location = data;         
                    }
                }
        );
    }


    };

by this piece of code:

<script>
            var ajaxFinished = 1;
            window.setInterval( "checkedForAnsweredCalls()", 1000 );            
        </script>

this ajax code access a struts2 action that has a json response type, so if some database conditions are met i get back an url.
thanks for the quick responses.

问题回答

利用火焰来描述您的网络应用,应当告诉你在什么地方出现hang。

至于多次Ajax申请,没有网页更新,你可能需要某种麻省式图书馆,无论是手写还是免费(prototype.js就是一例)。

我使用以下法典来管理我,因此不能对此表示赞扬。 我发现,法典的错误算法一环,一夫做了一些小改动。

var reqObj_arr = [];

function getXMLObject(index) {
    if(reqObj_arr[index]){
        return reqObj_arr[index];
    }

    var xmlHttp = false;
    try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
        } catch (e2) {
            xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
        }
    }
    if (!xmlHttp && typeof XMLHttpRequest !=  undefined ) {
        xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
    }
    reqObj_arr[index] = xmlHttp;
    return xmlHttp;  // Mandatory Statement returning the ajax object created
}

    function ajaxFunction(url, target, params) {
        var getdate = new Date();  //Used to prevent caching during ajax call
        var xmlhttp = new getXMLObject(target);    //xmlhttp holds the ajax object

        if(xmlhttp) {
            xmlhttp.open("POST",url,true); //calling url using POST method
            xmlhttp.onreadystatechange  = function() {
                if (xmlhttp.readyState == 4) {
                    if(xmlhttp.status == 200) {
                        document.getElementById(target).innerHTML = xmlhttp.responseText;
                    }
                    else {
                        alert("Error during AJAX call. Please try again");
                    }
                }
            };
            xmlhttp.setRequestHeader( Content-Type , application/x-www-form-urlencoded );
            if(params){
                xmlhttp.send(params); // Posting to PHP file
            }
            else{
                xmlhttp.send(); // Posting to PHP file
            }
        }
    }

用这个词,在你的网页上添加以下线 Java。

var url =  URL to the .php file ;
var target =  ID of the element to add retrieved data to ;
var params =  Any other parameters to send ;
ajaxFunction(url,target,params);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签