I was looking to do something with chess data results, where some will return immediately, but the last few results may return in 10-15 seconds. I created a quick little jQuery hack, but it does what I want (still not sure if it makes sense to use its tho):
(function($) { if (typeof $ !== 'function') return; $.longPull = function(args) { var opts = $.extend({ method:'GET', onupdate:null, onerror:null, delimiter:'\n', timeout:0}, args || {}); opts.index = 0; var req = $.ajaxSettings.xhr(); req.open(opts.method, opts.url, true); req.timeout = opts.timeout; req.onabort = opts.onabort || null; req.onerror = opts.onerror || null; req.onloadstart = opts.onloadstart || null; req.onloadend = opts.onloadend || null; req.ontimeout = opts.ontimeout || null; req.onprogress = function(e) { try { var a = new String(e.srcElement.response).split(opts.delimiter); for(var i=opts.index; i<a.length; i++) { try { var data = JSON.parse(a[i]);
Largely untested, but he seemed to be doing what you had in mind. I can think of all kinds of ways so that this might go wrong, though :-)
$.longPull({ url: 'http://localhost:61873/Test', onupdate: function(data) { console.log(data); }});
source share