替换的超时事件触发两次或两次以上(有时)
Replaced timeout event fires twice or more (sometimes)
我有一段内容,用户可以在双击后对其进行编辑。如果用户更改了内容,然后停止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();
});
}
事实证明(在上面评论员的帮助下)函数makeEditable
被调用了不止一次。
在函数开头添加以下两行代码修复了问题:
//if element is already editable - ignore
if(elem.attr("contentEditable") === "true")
return;
相关文章:
- Meteor Router数据函数被调用两次
- 从MySQL数据库中获取输入数据需要两次页面刷新
- Module.start()已激发两次
- Jquery点击事件必须点击两次
- Append元素在运行两次函数后不显示
- 防止双击执行两次jQuery post请求
- 我必须点击两次才能激活任何按钮操作(离子/角度)
- 单击jQuery会激发两次
- 如何避免在树上走两次
- button.单击两次删除附加操作后不工作
- 由于$compile,Javascript(Angular)嵌套指令加载了两次
- 单击元素两次后执行操作
- 替换的超时事件触发两次或两次以上(有时)
- addEventListener对所有元素都有效,但有时它会提醒消息两次或多次
- 如何修复有时加载两次、一次或根本不加载的 Facebook JSDK
- w3IncludeHTML有时包含两次
- 为什么Array.有时对某些值对进行两次排序比较
- PHP 表单代码有时同时执行两次
- Bootstrapx点击问题-需要两次点击,有时需要3次
- $.post()有时需要两次提交才能发送数据