Created
August 18, 2014 05:15
-
-
Save float1251/cd6f9a9b39b2603cbb47 to your computer and use it in GitHub Desktop.
ajax通信でリトライを行う
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
ajax通信エラー時にretryするplugin.. | |
timeoutの秒数は1通信に対しての秒数のため、 | |
最長 retryCount * timeoutミリ秒待つことになる. | |
@params url url文字列 | |
@params done 成功時のcallback | |
@params fail 失敗時のcallback | |
@params options ajaxのoption | |
### | |
do ($ = jQuery)-> | |
DEFAULT_RETRY_NUM = 3 | |
DEFAULT_TIMEOUT = 10000 | |
$.ajaxRetry = (url, done, fail, options = {retryCount: DEFAULT_RETRY_NUM, timeout: DEFAULT_TIMEOUT})-> | |
count = 0 | |
successCallback = (data)-> | |
done?(data) | |
options.retryCount ?= DEFAULT_RETRY_NUM | |
options.url = url | |
options.error = (data)-> | |
count++ | |
if count <= options.retryCount | |
$.ajax(this).done(successCallback) | |
else | |
fail?(data) | |
$.ajax(options).done(successCallback) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment