English 中文(简体)
setInterval alternative
原标题:

In my app I am polling the webserver for messages every second and displaying them in the frontend. I use setInterval to achieve this. However as long as the user stays on that page the client keeps polling the server with requests even if there is no data. The server does give an indication when no more messages are being generated by setting a variable. I thought of using this variable to clearInterval and stop the timer but that didn t work. What else can I use in this situation? I am using jquery and django. Here is my code:

jquery:
 var refresh = setInterval(
        function ()
            {
                var toLoad =  /myMonitor +  #content ;             
                $( #content ).load(toLoad).show();

            }, 1000); // refresh every 1000 milliseconds
    });

html:
div id=content is here 

I can access the django variable for completion in html with each refresh. How can I set clearInterval if at all ?

Note: stack overflow does not let me put is &gt &lt so html is incomplete

Thanks

Updated 03/16/2010 I must be doing something wrong. But cannot figure it out. Here is my script with clearTimer and it does not work.


var timer = null;
$(function(){
        if ("{{status}}" == "False")
        {
           clearInterval(timer);
        }
        else
        {
            timer = setInterval(
                function(){
                    var toLoad =  /myMonitor +  #content ;  
                    $( #content ).load(toLoad).show();} 
                    ,1000); // refresh every 1000 milliseconds
        }
    });

status is a boolean set in "views.py" (Django). Thanks a bunch.

问题回答

A couple people have already answered with specific resources to your problem, so I thought I would provide a bit of background.

In short, you want the server to push data to the browser to avoid extensive client-side polling. There isn t a good cross-browser way to support server push, so a common solution that requires much less polling is to use the Comet (another cleaning product, like AJAX) long-poll technique.

With Comet, the browser makes a request, and the server keeps the connection open without responding until new data is available. When the server does has new data, it sends it over the open connection and the browser receives it right away. If the connection times out, the browser opens a new one. This lets the server send data to the client as soon as it becomes available. As others have indicated, this approach requires special configuration of your web server. You need a script on the server that checks for data at an interval and responds to the client if it exists.

Something to keep in mind with this approach is that most web servers are built to get a request from a client and respond as quickly as possible; they re not intended to be kept alive for a long period of time. With Comet you ll have far more open connections than normal, probably consuming more resources than you expect.

Your clearInterval check is only checking when the document ready event is fired.

If the code you gave is exactly what s in the browser, then you re comparing the string "{{status}}" to the string "False". I d rather watch paint dry than wait for that to evaluate as true.

What if your requests taking longer than 1 second to complete? : You ll flood your server with requests.

function update () {
  $( #content ).show().load( /myMonitor +  #content , function (response, status) {
    if (!/* whatever you re trying to check*/) {
      setTimeout(update, 1000);
    };
  });
};

$(document).ready(function () {
  update();
});

Is closer than where you were, but you still need to work out how you re going to decide when you want to stop polling.





相关问题
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.

热门标签