0

I wrapped the function with setTimeout it will run forever. If you notice on the screenshot, js exception happened after so many times. That is what is happening on our real app too. I am not sure why.

Here is the fiddle: http://jsfiddle.net/ux12xoya/1/

    // JSON Request
    var auxTime = new Date();
    var jQueryCallbackRandom = auxTime.getTime();

    var callParameters = {
        url: 'http://jsfiddle.net/echo/jsonp/',
        timeout: 2,
        dataType: "jsonp",
        data: { echo: "Hello World!" },
        jsonpCallback: "jQueryRandom_" + jQueryCallbackRandom,
        success: function(){
         console.log("success");   
        },
        error: function(jqXHR, textStatus){
            console.log("failed with error: " + textStatus);
            window["jQueryRandom_" + jQueryCallbackRandom] = function() {
              window["jQueryRandom_" + jQueryCallbackRandom] = null;
            };
        }       
    };

    var timeout = setTimeout(callAjax, 5000)

    function callAjax() {
        $.ajax(callParameters);
        clearTimeout(timeout);
        timeout = setTimeout(callAjax, 5000)
    }

Here is the screenshot of the error: https://i.stack.imgur.com/fQ9hI.png

9
  • you have to put all variables in callAjax ... to makes them locals to callAjax scope
    – Hacketo
    Sep 11, 2015 at 20:08
  • You're using the same jQueryCallbackRandom with every request. If any one request takes longer than 5 seconds, you'll start running into conflicts. Stop manually naming the callback and that will stop happening.
    – Kevin B
    Sep 11, 2015 at 20:09
  • @KevinB see : stackoverflow.com/questions/32515179/…
    – Hacketo
    Sep 11, 2015 at 20:10
  • 1
    No it isn't, you set it once, at the top, and then reused it for every request.
    – Kevin B
    Sep 11, 2015 at 20:12
  • 1
    The callParameters are just initialized once, so they'll be exactly the same on each $.ajax() invocation. If you move all that stuff (exception the start-off call to setTimeout() inside the callAjax() function as @Hacketo suggested, then it will not have those failures. (It will still have timeout errors, because you've set a very short timeout limit.)
    – Pointy
    Sep 11, 2015 at 20:16

1 Answer 1

2

This is failing because your timeout is too short. When the request times out, the callback is removed from the window, and then when the request actually finishes, an exception is thrown because the callback isn't on the window. It doesn't happen every time because you're using the same callback name for every request, and sometimes it just so happens to complete while another request hasn't timed out yet resulting in a "success" that wasn't actually a success.


Your code was actually pretty close, the root of the issue here is that you didn't give each request a unique JSONPCallback. You simply need to move those variables into the ajax function so that they will be re-created for each request, resulting in each getting it's own auxTime based callback name.

// JSON Request
var timeout = setTimeout(callAjax, 5000)

function callAjax() {
    var auxTime = new Date();
    var jQueryCallbackRandom = auxTime.getTime();

    var callParameters = {
        url: 'http://jsfiddle.net/echo/jsonp/',
        timeout: 5,
        dataType: "jsonp",
        data: { echo: "Hello World!" },
        jsonpCallback: "jQueryRandom_" + jQueryCallbackRandom,
        success: function(){
         console.log("success");   
        },
        error: function(jqXHR, textStatus){
            console.log("failed with error: " + textStatus);
            window["jQueryRandom_" + jQueryCallbackRandom] = function() {
              window["jQueryRandom_" + jQueryCallbackRandom] = null;
            };
        }       
    };
    $.ajax(callParameters);
    clearTimeout(timeout);
}

http://jsfiddle.net/ux12xoya/2/

23
  • The objective of this poc is to capture the Uncaught TypeError. Here in our product, our timeout is 60 seconds and it still happens rarely
    – devwannabe
    Sep 11, 2015 at 20:16
  • The error will always occur if the timeout happens, because this is JSONP. it is unavoidable. If the callback is no longer available to be executed, a typeerror will occur.
    – Kevin B
    Sep 11, 2015 at 20:17
  • 1
    it's not a bug at all, it's just the way it works. JSONP is in of itself hacky, it isn't an official protocol supported by browsers, instead it's a clever workaround to allow cross-origin transfer of information without the pitfalls of xhr through the use of <script> tags, which are allowed to be cross-origin.
    – Kevin B
    Sep 11, 2015 at 20:26
  • 1
    i'd have to actually see the code. If it's the same as in the question, it's certainly possible for it to be not unique.
    – Kevin B
    Sep 11, 2015 at 20:39
  • 1
    If i was going to add an auto-retry feature, i would do it in such a way that it can be toggled on and off per request. Then you can just filter it to not affect jsonp requests, and problem solved.
    – Kevin B
    Sep 11, 2015 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.