【发布时间】:2023-04-10 05:00:01
【问题描述】:
我有一段内容允许用户在双击它时对其进行编辑。如果用户更改内容后停顿2秒,则将更新后的内容发送到服务器进行保存。
为此,我已将input 事件侦听器绑定到开始 2 秒倒计时的那个部分,如果已经有倒计时,则前者将被取消并开始一个新的倒计时。在倒计时结束时,会向服务器发送一个带有新数据的 http POST 请求。
问题是,有时在倒计时结束时,我会看到发送了 2 个或更多请求,就好像在插入新请求之前没有取消倒计时一样,我不知道为什么。
有问题的代码如下:
//this function is bound to a double-click event on an element
function makeEditable(elem, attr) {
//holder for the timeout promise
var toSaveTimeout = undefined;
elem.attr("contentEditable", "true");
elem.on("input", function () {
//if a countdown is already in place, cancel it
if(toSaveTimeout) {
//I am worried that sometimes this line is skipped from some reason
$timeout.cancel(toSaveTimeout);
}
toSaveTimeout = $timeout(function () {
//The following console line will sometimes appear twice in a row, only miliseconds apart
console.log("Sending a save. Time: " + Date.now());
$http({
url: "/",
method: "POST",
data: {
action: "edit_content",
section: attr.afeContentBox,
content: elem.html()
}
}).then(function (res) {
$rootScope.data = "Saved";
}, function (res) {
$rootScope.data = "Error while saving";
});
}, 2000);
});
//The following functions will stop the above behaviour if the user clicks anywhere else on the page
angular.element(document).on("click", function () {
unmakeEditable(elem);
angular.element(document).off("click");
elem.off("click");
});
elem.on("click", function (e) {
e.stopPropagation();
});
}
【问题讨论】:
-
您是否尝试将
console.log添加到if(toSaveTimeout) {块中以查看它是否进入其中? -
每次调用
makeEditable函数时,您的 toSaveTimeout 都是未定义的,因为它绑定到此函数范围。您的测试if(toSaveTimeout)将始终失败。 -
确保您不要两次调用
makeEditable -
@Franck:我认为用户只会双击那些。所以用户双击,然后开始输入输入。每次输入输入后,它应该等待 2 秒并向服务器发送请求。如果用户在 2 秒之前再次输入,它应该取消 2 秒计时器。对我来说就是这样。
-
好的,是的 :-(。这就是问题所在。当元素已经处于可编辑状态时,双击并再次双击似乎是问题的唯一原因。谢谢
标签: javascript angularjs settimeout