gpt4 book ai didi

javascript - 函数仅在调试器中有效

转载 作者:行者123 更新时间:2023-11-29 18:01:27 25 4
gpt4 key购买 nike

我编写的函数用于检查按钮是否被单击两次以及是否要测量两次单击之间的时间。它必须防止短时间内多次点击。

按钮点击:

$("#Save").click(function () {
dateTime1 = new Date().getTime();
BtnId = this.id;
showSaveDialog();
});

和测量函数:

ButtonWasTriggeredTwice: function () {
var result = false;
var currentTime = new Date().getTime();
var time = currentTime - dateTime1;
if (PreviousBtn === null) {
result= false;
} else {
if (PreviousBtn === BtnId) {
if ( time < 1500) {
result = true;
}
else result = false;
}
else {
result= false;
}
}
PreviousBtn = BtnId;
BtnId = null;
return result;
}

BtnId 和 PreviosusBtn 是全局范围变量。

奇怪的是,当我在调试器中设置断点时,这个函数工作得很好。如果我在每次点击按钮时关闭调试器功能 block ,无论点击之间的时间间隔是多少

最佳答案

您可以将此解决方案与解除绑定(bind)和超时一起使用,如下所示:

HTML

<input type="button" id="Save" value="save me" />

JS:

function saveEventButton(){
$("#Save").click(function () {
alert('saved!');
$("#Save").unbind('click');
setTimeout(function(){
saveEventButton();
}, 5000); // 5sec
});
}

saveEventButton();

This is the JSFiddle

更新此解决方案是我的和 Revish Patel 解决方案的混合

function disableTimeout(_this){
$(_this).prop('disabled','disabled');
setTimeout(function(){
$(_this).prop('disabled','');
}, 5000); // 5sec
}


$("#Save").click(function () {
alert('saved!');
disableTimeout(this);
});

This is the JSfiddle

关于javascript - 函数仅在调试器中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34678223/

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