跳到主要內容

發表文章

目前顯示的是 6月, 2014的文章

Polling with JQuery deferred (en)

Polling with JQuery deferred (en) Problem Objective Ver 0.1 Ver 0.2 (Generalization) Problem Imagine you want to get some web pages via JQuery Ajax, but without knowing how many pages until you actually fetch them. In such case, you can’t aggregate deferred objects before fetching. Available solution is polling pages instead. That is, fetch a page and see if more pages to go. If so, continue to retrieve another one, otherwise stop polling. Let’s assume an API for polling page. e.g. http://example.com/api/page?p=n ` and request it several time with consecutive n . i.e. var api = 'http://example.com/api/page' ; $ . get ( api , { p : 0 }) . then ( function ( data ) { if ( is_more ( data )) { return $ . get ( api , { p : 1 }); } return data ; }) . then (...) ; Now you see the problem I’m gonna shoot it - how to resolve the last then() with polled pages? Objective Prototype: /** * @param api URL f