最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - AJAX constantly look for server update? Javascript - Stack Overflow

programmeradmin6浏览0评论

I have a situation where a display on a webpage needs to be updated at random. I am wanting to do this in AJAX but am not sure how to do this other than to do a

while(true) { ajaxFunction(); sleep(1) }

Type thing.

The problem with this is that the webpage needs to be updated very quickly on a change to the server, but the changes could happen very sporadically sometimes never.

EDIT: This is an Iphone application using a UIWebView, is it possible to use the iPhone's push notification to interface with the javascript?

Thanks!

I have a situation where a display on a webpage needs to be updated at random. I am wanting to do this in AJAX but am not sure how to do this other than to do a

while(true) { ajaxFunction(); sleep(1) }

Type thing.

The problem with this is that the webpage needs to be updated very quickly on a change to the server, but the changes could happen very sporadically sometimes never.

EDIT: This is an Iphone application using a UIWebView, is it possible to use the iPhone's push notification to interface with the javascript?

Thanks!

Share Improve this question edited Aug 17, 2009 at 18:13 DevDevDev asked Aug 17, 2009 at 18:07 DevDevDevDevDevDev 5,1778 gold badges58 silver badges89 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

I think what you're describing is Comet and there are a couple of plugins for jQuery:

http://code.google./p/jqueryet/

http://plugins.jquery./project/Comet

The only way I can think of is to constantly query it. Just keep your responses small so you're not moving a bunch of data around for no reason. You could even suspend the response from the server until data is available.

setInterval(function(){
  $.get("updates.php", function(result) {
    alert(result);
  });
}, 5000);

Might even build some logic into updates.php to cancel the setInterval() after 10 minutes of inactivity on the users part. That will kill off constant requests from users who are no longer logged in.

You should consider implementing some kind of a et (server push) technology, when you want to optimize the servers load. If you only have a few users, than a polling solution is suitable.

About et: et technologies are nothing else, but making a simple http request to the server, where the server does not respond to the request immediately, but waits until there is something to respond with. Until than the thread on the server is suspended.

There are some technical aspects you should consider when implementing a server push technology (like where should I suspend the thread). It is best to use an open source one. It is easy to find them on the web if you search for et.

If the changes are sporadic, consider not using any AJAX. Wait for the user to refresh or revisit the page.

发布评论

评论列表(0)

  1. 暂无评论