Process a continuous stream of JSON

前端 未结 4 1330
耶瑟儿~
耶瑟儿~ 2020-11-30 19:16

The (now defunct) page http://stream.twitter.com/1/statuses/sample.json used to return a continuous and endless stream of JSON data.

I\'d like to process it using jQ

4条回答
  •  天涯浪人
    2020-11-30 20:11

    A web page at a very fundamental level can't keep a live/running connection to a server. Web browser sends a request to the server. Server sends a response (the HTML and more) back to the client (web browser). Think of this as a stateless model - no connection is ever kept alive after the request and response have been completed.

    Therefore, you have to do it yourself. You have to invoke additional, periodic requests from the client-side.

    One way would be to periodically call your AJAX/GET functionality via setInterval() function. For example:

    setInterval(function() {
    
        $.ajax({
          url: "mydata/get",
          success: function(){
            // update content.
          }
        });
    
    }, 5000);
    

    This will fire off an AJAX request to mydata/get (or whatever URL you want to use) every 5 seconds.

提交回复
热议问题