Skip to content

Instantly share code, notes, and snippets.

@float1251
Created August 18, 2014 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save float1251/cd6f9a9b39b2603cbb47 to your computer and use it in GitHub Desktop.
Save float1251/cd6f9a9b39b2603cbb47 to your computer and use it in GitHub Desktop.
ajax通信でリトライを行う
###
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