Hey,
I wrote this class for cache ajax request if the same request sent twice.
The second reason why i use this class is a like there is only one exit point
from the application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | var Communicator =(function(){ var instantiated; var retry = new Array(); var cached_data = new Array(); function init (){ return { dispatch:function(url, callback){ var self = this; if (Communicator.getInstance().cached_data[url]) { callback(Communicator.getInstance().cached_data[url]); return; } if (!retry[url]) { retry[url] = 0; } var _rq = $.ajax({ url: url, timeout: 30000, success: function(_data){ Communicator.getInstance().cached_data[url] = _data; callback(_data); }, error: function(){ if (retry[url] < 3) { retry[url]++; dispatch(url, callback, retry[url]); } else { callback(""); } } }); }, cached_data: {} } } return { getInstance :function(){ if (!instantiated){ instantiated = init(); } return instantiated; } } })() |
Using the class
1 2 3 4 5 6 7 8 9 10 11 12 | Communicator.getInstance().dispatch(url,function(data){ var error = false; try { data = eval("("+data+")"); } catch(err) { error = true; } if (!error) { console.log(data); } }); |
Have fun ![]()
Garry
When the console is close and we call console.log from javascript most of the times
My blog has a new home
After a long time that my blog suffered from a slow and down times at GoDaddy.
I`m happy to announce that we found new home at XLHost.
Sooo…
We Are Back !!!