gpt4 book ai didi

javascript - 是什么导致我的 jQuery 调用出现这种递归?

转载 作者:行者123 更新时间:2023-12-03 00:08:25 26 4
gpt4 key购买 nike

我目前编写了一些代码,这些代码将在自定义 .trigger() 调用后调用函数。这就是所谓的:

function _visitAddTag(state, listener) {
if (state.properties.action = "RedoAddTag") contextMenu.unhide();
var $target = contextMenu.getContextMenuUI().tags; // Grab tag elements
var callback = function () {
if (listener) {
google.maps.event.removeListener(listener);
}
$target.off("tagIds-updated", callback);
contextMenu.hide();
next.call(contextMenu.getTargetLabel(), state.transition);
};
$target.on("tagIds-updated", callback());
}

next.call() 行导致触发此方法:

function () { // 'this' is contextMenu.getTargetLabel(), as called in Onboarding.js/_visitAddTag()
tracker.push('Onboarding_Transition', {onboardingTransition: "tag-attribute-1"});
var tags = this.getProperty('tagIds');
return tags == [2] ? "adjust-heading-angle-1" : "redo-tag-attribute-1" // Where 2 is the tag_id of the "points into traffic" tag
}

返回值“redo-tag-attribute-1”将导致整个过程再次循环。

我在这里看到了递归的可能性,特别是如果“tagIds-updated”事件在第二个函数调用内部以某种方式触发。但是,当我调试代码时,该事件仅触发一次。有人对这里发生的事情有任何想法吗?我是一个 javascript 新手,所以也许我错过了一些明显的东西。

最佳答案

我无法完全理解代码。我目前在您的代码中发现两个问题:

  1. $target.on("tagIds-updated",callback()); 并没有真正附加事件处理程序。它只是立即调用callback。您需要$target.on("tagIds-updated",callback);。请注意缺少的 ()
  2. tags == [2] 将始终为 falseWhy? 。您也许可以使用 tags.includes(2) 代替。按照目前的情况,该函数将始终返回“redo-tag-attribute-1”。这可能是您面临的递归的根本原因。

关于javascript - 是什么导致我的 jQuery 调用出现这种递归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54858887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com