{"id":1398,"date":"2011-07-10T16:46:37","date_gmt":"2011-07-10T15:46:37","guid":{"rendered":"http:\/\/blogs.ukoln.ac.uk\/ukolndev\/?p=1398"},"modified":"2013-06-08T13:36:37","modified_gmt":"2013-06-08T13:36:37","slug":"jquery-plugin-polling-with-timeouts","status":"publish","type":"post","link":"https:\/\/www.emmatonkin.com\/ukolndev\/2011\/07\/10\/jquery-plugin-polling-with-timeouts\/","title":{"rendered":"JQuery Plugin: Polling with timeouts"},"content":{"rendered":"<p>Polls a server resource at specified timeout interval until abortTimeOut is reached with options for testing responses for success.<\/p>\n<p>This was useful for allowing a machine a certain amount of time to wake from hibernate before aborting.<\/p>\n<pre lang=\"JavaScript\" line=\"1\">\r\n \/**\u00a0\r\n * PollingWithTimeouts - Polls a server resource at specified timeout \r\n * interval until abortTimeOut is reached\r\n *\r\n * Settings -:\r\n * url -          the url to ajax call\r\n * method -       get\/post (defaults to get)\r\n * sendData -     array of values to be passed in - \r\n *                e.g. {name: \"foo\", something: \"else\"}\r\n * timeout -      timeout in ms between each poll (default 1000)\r\n * type -         json\/text\/xml what you are expecting as a \r\n *                response (default json)\r\n * abortTimeOut - how long in total before we completely give up in \r\n *                milliseconds (default 60000) to have no abort \r\n *                timeout dont pass in abortCallBack or testCallBack\r\n *\r\n *\r\n * Usage -:\r\n *\r\n * 1) As a never ending poll\r\n * $(document).ready(function(){\r\n *       $.PollingWithTimeouts({\r\n *              url : '\/foo\/',\r\n *              sendData:  {foo: 'foo', etc: 'etc'}\r\n *       });\r\n * })\r\n *\r\n * 2) As a never ending poll with a pollCallBack (stock ticker for example)\r\n * $(document).ready(function(){\r\n *       $.PollingWithTimeouts({\r\n *              url : '\/foo\/',\r\n *              sendData:  {foo: 'foo', etc: 'etc'}\r\n *       },\r\n *       function(pollCallBackData) { alert(pollCallBackData); }\r\n *   );\r\n * })\r\n *\r\n * 3) As a poll with an (optional) pollCallBack that aborts after the \r\n * specified time calling back to the (optional) abortCallBack\r\n * \r\n * $(document).ready(function(){\r\n *       $.PollingWithTimeouts({\r\n *              url : '\/foo\/',\r\n *              sendData:  {foo: 'foo', etc: 'etc'},\r\n *              abortTimeOut: 60000\r\n *       },\r\n *       function(pollCallBackData) { alert(pollCallBackData); },\r\n *       function(abortCallBackData) { alert(abortCallBackData); },\r\n *   );\r\n * })\r\n *\r\n * 4) As a poll with a call back that aborts after the specified time \r\n * calling back to the abort function isCompletedCallBackData allows the \r\n * caller to test response data and return true if polling is complete\r\n *    If isCompletedCallBack is true it calls the successCallBack\r\n *    e.g. (and the reason I wrote it) after doing WOL on \r\n * machine poll the server to ping and determine if the machine is awake. \r\n * This needs to abort after a period of time.\r\n * $(document).ready(function(){\r\n *     $.PollingWithTimeouts({\r\n *              url : '\/foo\/',\r\n *              sendData:  {foo: 'foo', etc: 'etc'},\r\n *              abortTimeOut: 60000\r\n *     },\r\n *     function(pollCallBackData) { alert(pollCallBackData); },\r\n *     function(abortCallBackData) { alert(abortCallBackData); },\r\n *     function(isCompletedCallBackData) {alert(isCompletedCallBackData);},\r\n *     function(successCallBackData) { alert(successCallBackData); },\r\n *   );\r\n * })\r\n *\r\n * Copyright (c) 2011 UKOLN (http:\/\/www.ukoln.ac.uk)\r\n * Mark Dewey\r\n * Licensed under GPL:\r\n * http:\/\/www.gnu.org\/licenses\/gpl.html\r\n *\r\n * Version: 0.9\r\n *\/\r\n\r\n(function($) {\r\n    $.PollingWithTimeouts = function(options, pollCallBack, \r\n                      abortCallBack, isCompletedCallBack, successCallBack) {\r\n\r\n            settings = jQuery.extend({\r\n                url: '',        \/\/ URL of ajax request\r\n                method: 'get', \/\/ get or post\r\n                sendData: '',   \/\/ array of values to be passed in \r\n                                \/\/e.g. {name: \"foo\", something: \"else\"}\r\n                timeout: 1000,   \/\/ timeout in milliseconds - 1 sec\r\n                type: 'json', \/\/or whatever else you like\r\n                abortTimeOut: 60000  \/\/1 min\r\n            }, options);\r\n\r\n                f = settings.method == 'post' \r\n                     || settings.method == 'POST' ? $.post : $.get;\r\n\r\n                var abort = new Date().getTime() + settings.abortTimeOut;\r\n                getdata();\r\n\r\n                function getdata()\r\n        {\r\n\r\n                        f(settings.url, settings.sendData, function(d){\r\n\r\n                if (abortCallBack &amp;&amp; \r\n                          new Date().getTime() &gt; abort) {\r\n                        clearTimeout(PollingWithTimeouts);\r\n                        abortCallBack(d);\r\n                }\r\n                else {\r\n                        if (isCompletedCallBack) {\r\n                                if (isCompletedCallBack(d)) {\r\n                                        clearTimeout(PollingWithTimeouts);\r\n                                        if (successCallBack) {\r\n                                                successCallBack(d);\r\n                                        }\r\n                                } else {\r\n                                        if (pollCallBack) {\r\n                                                pollCallBack(d);\r\n                                        }\r\n                                        PollingWithTimeouts \r\n                                           = setTimeout(getdata, \r\n                                               settings.timeout);\r\n                                }\r\n                        } else {\r\n                                if (pollCallBack) {\r\n                                        pollCallBack(d);\r\n                                }\r\n                                PollingWithTimeouts = setTimeout(getdata, \r\n                                                         settings.timeout);\r\n                        }\r\n                }\r\n            }, settings.type)\r\n        }\r\n\r\n        };\r\n})(jQuery);\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Polls a server resource at specified timeout interval until abortTimeOut is reached with options for testing responses for success. This was useful for allowing a machine a certain amount of time to wake from hibernate before aborting. \/**\u00a0 * PollingWithTimeouts &#8211; Polls a server resource at specified timeout * interval until abortTimeOut is reached * [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":1445,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[132,133],"_links":{"self":[{"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/posts\/1398"}],"collection":[{"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/comments?post=1398"}],"version-history":[{"count":3,"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/posts\/1398\/revisions"}],"predecessor-version":[{"id":1855,"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/posts\/1398\/revisions\/1855"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/media\/1445"}],"wp:attachment":[{"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/media?parent=1398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/categories?post=1398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/tags?post=1398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}