JS异常仍然可以潜入,但很少,不确定为什么 [英] JS exception is still able to sneak in but very rarely, not sure why

查看:80
本文介绍了JS异常仍然可以潜入,但很少,不确定为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用setTimeout包装了该函数,它将永远运行.如果您在屏幕截图上注意到,则js异常发生了很多次.这也是在我们的真实应用程序上发生的事情.我不确定为什么.

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.

这是小提琴: http://jsfiddle.net/ux12xoya/1/

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)
    }

以下是错误的屏幕截图: http://i.imgur.com/IFiW9ij.png

Here is the screenshot of the error: http://i.imgur.com/IFiW9ij.png

推荐答案

此操作失败,因为您的超时时间太短.当请求超时时,回调将从窗口中删除,然后当请求实际完成时,由于回调不在窗口中而引发异常.并非每次都发生这种情况,因为您为每个请求使用了相同的回调名称,有时恰好在另一个请求尚未超时但又导致成功"实际上并不成功的情况下完成了该回调

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.

您的代码实际上非常接近,问题的根源在于您没有为每个请求提供唯一的JSONPCallback.您只需要将这些变量移到ajax函数中,以便将为每个请求重新创建它们,从而使每个变量都具有自己的基于auxTime的回调名称.

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/

这篇关于JS异常仍然可以潜入,但很少,不确定为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆