如何防止 jQuery 函数在前面的函数失败时不运行

How to prevent jQuery functions from not running if a preceding function fails?

本文关键字:函数 运行 失败 在前面 何防止 jQuery      更新时间:2023-09-26

如果前面的函数失败,我遇到了jQuery函数无法运行的问题,例如:

jQuery(function($) {
    // this function failed to run because the dependency file wasn't loaded
});

jQuery(function($) {
    // fails to run unless all preceding function succeed
});

jQuery(function($) {
    // fails to run unless all preceding function succeed
});

这样做是因为它们都是一小块代码,每个代码块都用于我网站上使用的 jquery 插件,所以一个用于滑块,另一个用于下拉菜单......,例如,如果一个没有滑块的页面我没有加载滑块插件,所以滑块函数之后的所有内容都无法运行。

那么我该怎么办,所以jquery忽略了失败的功能?

并提前感谢。

使用

catch 块来捕获错误,或者首先使用 if s 来避免它。