gpt4 book ai didi

JavaScript "Don´t change states to fast"

转载 作者:行者123 更新时间:2023-12-03 01:40:39 26 4
gpt4 key购买 nike

嘿伙计们,我有一个代码,当您单击按钮时,它会从一种状态更改为另一种状态(它会启动视频并混合)。现在我尝试说我的事件处理程序如果有人点击速度非常快,他不应该将其用作输入。像 = Eventhandler 这样的东西请注意一秒钟内只需单击一次。希望我的代码可以理解:

<script>
var clickState = 0;
var btn = document.querySelector('#playbutton');

btn.addEventListener('click', function(){

if (clickState == 0) {
document.querySelector('#toggler').emit('fade_1');

var videoEl_1 = document.querySelector('#video');

videoEl_1.play();
document.querySelector( "#skyid" ).emit('fade_1');
clickState = 1;

} else {

document.querySelector('#toggler').emit('fade_2');

var videoEl_1 = document.querySelector('#video');

videoEl_1.pause();
document.querySelector( "#skyid" ).emit('fade_2');
clickState = 0;
}
});
</script>

最佳答案

您可以在单击按钮时禁用该按钮,然后设置超时以在一秒钟后重新启用它。

像这样

$( document ).ready(function() {
$("#myButton").click(function(){
// disable the button
$("#myButton").prop("disabled", true);

//do the things you want the button to do:
console.log("doing stuff");

// reenable the button after 1 second
setTimeout(function(){
$("#myButton").prop("disabled", false);
}, 1000);
});
});

示例如下:

https://jsfiddle.net/20n1gb89/8/

我在这里使用了一些 jQuery,但 setTimeout 是原生 JavaScript

编辑:

您似乎正在为同一按钮的单击处理程序定义一个单击处理程序。看我的评论。删除 btn.addEventListener 并仅保留 if else 语句。看看是否有效。

 $(document).ready(function () {
// here you define a click handler for playbutton
$("#playbutton").click(function () {
// disable the button
$("#playbutton").prop("disabled", true);

//do the things you want the button to do:
var clickState = 0;
var btn = document.querySelector('#playbutton');

// here you define a click handler for the same button
// inside the first click handler. You shouldn't do that.
btn.addEventListener('click', function () {
if (clickState == 0) {
document.querySelector('#toggler').emit('fade_1');

var videoEl_1 = document.querySelector('#video');

videoEl_1.play();
document.querySelector("#skyid").emit('fade_1');
clickState = 1;
} else {
document.querySelector('#toggler').emit('fade_2');

var videoEl_1 = document.querySelector('#video');

videoEl_1.pause();
document.querySelector("#skyid").emit('fade_2');
clickState = 0;
}

console.log("doing stuff");

// reenable the button after 1 second
setTimeout(function () {
$("#playbutton").prop("disabled", false);
}, 2000);
});

});
});

编辑2:

就是这样。试试这个:

$(document).ready(function () {
$("#playbutton").click(function () {
// disable the button
$("#playbutton").prop("disabled", true);

//do the things you want the button to do:

var clickState = 0;

// this doesn't really make sense. clickState will always be 0
// as it is defined as 0 each time you click the button. You
// will need to define clickState outside the click handler
// for this to work.
if (clickState == 0) {
document.querySelector('#toggler').emit('fade_1');

var videoEl_1 = document.querySelector('#video');

videoEl_1.play();
document.querySelector("#skyid").emit('fade_1');
clickState = 1;
} else {
document.querySelector('#toggler').emit('fade_2');

var videoEl_1 = document.querySelector('#video');

videoEl_1.pause();
document.querySelector("#skyid").emit('fade_2');
clickState = 0;
}

console.log("doing stuff");

// reenable the button after 1 second
setTimeout(function () {
$("#playbutton").prop("disabled", false);
}, 2000);
});

});

});

关于JavaScript "Don´t change states to fast",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50857077/

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