热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

首先失败的jQuery.ready()打破了其余部分-FirstfailingjQuery.ready()breakstherest

WeallowuserstowritecodewhichsometimescallsjQuery.ready(function(){..})multipletimes.Its

We allow users to write code which sometimes calls jQuery.ready(function(){..}) multiple times. It seems like the first function call that throws an error prevents execution of the rest of the functions.

我们允许用户编写有时多次调用jQuery.ready(function(){..})的代码。看起来第一个抛出错误的函数调用会阻止执行其余的函数。

There was a discussion about this here with the solution wrapped jQuery.ready() in a delegate instead of wrapping the anonymous function parameter passed to jQuery.ready(..).

这里有一个关于这个的讨论,解决方案在委托中包装jQuery.ready()而不是包装传递给jQuery.ready(..)的匿名函数参数。

How can I override jQuery.ready(fn) and wrap the passed-in function parameter in a delegate which wraps it in try/catch and the passes to jQuery.ready(delegate)?

我怎样才能覆盖jQuery.ready(fn)并将传入的函数参数包装在一个委托中,该委托将它包装在try / catch中并传递给jQuery.ready(委托)?

Here is an example:

这是一个例子:


  



  

What can I do to make code in run regardless of errors in ?

无论错误是什么,我该怎么做才能使代码运行?

3 个解决方案

#1


7  

If you need to catch your own errors, then catch them with your own try/catch:

如果您需要捕获自己的错误,请使用您自己的try / catch捕获它们:

$(document).ready(function() {
   try {
       // put your normal code here
   } catch (e) {
       // put any code you want to execute if there's an exception here
   }
});

This will allow all subsequent code to continue without pause. One might ask why you're getting errors thrown? And perhaps what you should be doing to fix that or handle that more directly?

这将允许所有后续代码继续而不会暂停。有人可能会问你为什么会被抛出错误?或许你应该做些什么来解决这个问题或者更直接地处理它?

If you want, you could do this and replace all the troublesome calls to jQuery.ready() with jQuery.safeReady():

如果你愿意,你可以这样做,用jQuery.safeReady()替换所有对jQuery.ready()的麻烦调用:

jQuery.fn.safeReady = function(f) {
    jQuery.ready(function() {
        try {
            f();
        } catch(e) {
            // make it so you can see errors in the debugger 
            // so you would know if something was wrong
            if (window.console) {
                console.log(e);
            }
        }
    });
};

#2


1  

You can overwrite it like that:

你可以像这样覆盖它:

jQuery.fn.ready = function(param){
    // do whatever you wish
};

but you should not do it. See this jsfiddle for a reason.

但你不应该这样做。看到这个jsfiddle是有原因的。

The example from my jsfiddle shows, that the above solution would also overwrite commonly used jQuery feature and can severely break your code.

我的jsfiddle的例子表明,上面的解决方案也会覆盖常用的jQuery功能,并且会严重破坏你的代码。

Just rewrite your code - either by changing .ready() into .delegate() or by enclosing poorly written code within try {...} catch(...) {...} statements.

只需重写代码 - 或者通过将.ready()更改为.delegate(),或者在try {...} catch(...){...}语句中包含写得不好的代码。

#3


1  

It's worth knowing that in JQuery 3.0 this behavior has changed, and one error in JQuery.ready() will not prevent the rest from firing.

值得一提的是,在JQuery 3.0中,这种行为发生了变化,JQuery.ready()中的一个错误不会阻止其余的触发。

See official documentation here:

请参阅此处的官方文档

https://github.com/jquery/jquery/issues/1823

https://github.com/jquery/jquery/issues/1823

https://jquery.com/upgrade-guide/3.0/#breaking-change-document-ready-handlers-are-now-asynchronous

https://jquery.com/upgrade-guide/3.0/#breaking-change-document-ready-handlers-are-now-asynchronous


推荐阅读
author-avatar
阿凯宜_
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有