开发者

Dynamic increasing counter

I was wondering what would be the "best" way of making an counter in javascript or jQuery that would respond to data from a database? The purpose is that it should show the amount of people currently taking part of surveys in different 开发者_如何学运维countries. I guess I would need to hook it to sán Ajax request and maybe do some kind of long polling solution that "listens" for updates in the database. Any good pointers to this?

Regards!


The BEST way would be using comet, which allows to have "server events" that are pushed to the client (as facebook and many other sites do), but it's relatively hard to implement.

The cometd project

One easy way, would be using setInterval.

setInterval(function(){
   $.ajax({
     ...
   });
}, 5000);

Hope this helps


Do you really need to update this value dynamically or is it sufficient to get the value on page load?

If it is necessary, then yes, make an ajax request to fetch the number.

EDIT:

And make the request on regular intervals, using for instance setInterval.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜