patternjavascriptMinor
jQuery AJAX requests - Plugin to Backoff on failure
Viewed 0 times
ajaxpluginjquerybackofffailurerequests
Problem
I'm brand new here, but I've read the guidelines and looked at some up-voted questions. Hopefully I understand how I'm supposed to do this... if not, please let me know and I'll fix my question.
Explanation
I have written a simple javascript plugin of sorts. It takes the settings object for a jQuery.ajax request and a few options (frequency, failure tolerance, etc) and then periodically issues the ajax request. If the request fails, it increases the interval to the next request. If success follows a number of failures, the interval is slowly reduced to reach the original frequency.
Usage looks something like this:
Purpose of Review
I'm open to any feedback about my code that you're willing to give me. However, I came here seeking a few things specifically:
The Code
``
//==============================================
// Combine user options with our defaults
var opts = $.extend({
frequency: 1000, // How often to issue ajax request
ceiling: 10, // After this number of failures, multiple will not be applied
mult
Explanation
I have written a simple javascript plugin of sorts. It takes the settings object for a jQuery.ajax request and a few options (frequency, failure tolerance, etc) and then periodically issues the ajax request. If the request fails, it increases the interval to the next request. If success follows a number of failures, the interval is slowly reduced to reach the original frequency.
Usage looks something like this:
var ajax = {url: 'example.com', success: mySuccessFunc};
var cw = Coward(ajax, {}); // Request my url with default settings
cw.Start();
... // Coward is running, refreshes happening periodically
cw.Stop();Purpose of Review
I'm open to any feedback about my code that you're willing to give me. However, I came here seeking a few things specifically:
- Are there any bugs that will result in an excessive number of requests or other undesirable behavior? Am I doing anything unsafe? Race conditions?
- JSLint warns me about scope in a few places, but the code works. Am I violating standards? Should I be handling scope differently (particularly with the 'underscore' functions and the 'live variables')?
The Code
``
// Coward - One time or periodic refresh that retreats if anything goes wrong.
//
// This tool was designed for use with jQuery 1.9 or later
//
// Written by Adam Jensen
function Coward(userReq, custom) {
'use strict';
//==============================================
// Options and variables used by the coward
// - Do not change opts` after invocation -//==============================================
// Combine user options with our defaults
var opts = $.extend({
frequency: 1000, // How often to issue ajax request
ceiling: 10, // After this number of failures, multiple will not be applied
mult
Solution
You should check that the supplied
The way you have it now, if a user uses the following configs :
will cause the check below to pass, resulting in an error
success and error callbacks are functions not just that they exist.if (typeof userReq.success === "function") {
var successCallback = userReq.success;
delete userReq.success;
}The way you have it now, if a user uses the following configs :
var ajax = {url: 'example.com', success: null};
var ajax = {url: 'example.com', success: "my success function"};will cause the check below to pass, resulting in an error
if (successCallback !== undefined) {
successCallback(data, status, jqxhr);
}Code Snippets
if (typeof userReq.success === "function") {
var successCallback = userReq.success;
delete userReq.success;
}var ajax = {url: 'example.com', success: null};
var ajax = {url: 'example.com', success: "my success function"};if (successCallback !== undefined) {
successCallback(data, status, jqxhr);
}Context
StackExchange Code Review Q#106662, answer score: 2
Revisions (0)
No revisions yet.