按键功能中的setTimeout功能如何工作
How to work once setTimeout function that in keypress function
我希望我的按键功能在每次按下时都能正常工作,但 setTimeout 不起作用。这是我的 html 和脚本;
html:
<div id="fake" class="full-height col-md-3 col-sm-12 col-xs-12">
<div class="nav-right">
<div class="form-search">
<input id="fake-search" onkeypress="dialogTrigger(event)" class="fake-search" type="text" />
</div>
</div>
</div>
脚本:
function dialogTrigger(event) {
var keyboardInput = event.which;
keyboardInput = keyboardInput + 5;
var changedInput = String.fromCharCode(keyboardInput);
$("#fake-search").val($("#fake-search").val() + changedInput);
setTimeout(function () {
doSomething(); // about html
}, 10000);
}
你需要决定逻辑是什么。如果这个想法是限制一个对话框在 10 秒后打开一次,那么你需要做这样的事情
var to;
function dialogTrigger(event) {
var keyboardInput = event.which;
keyboardInput = keyboardInput + 5;
var changedInput = String.fromCharCode(keyboardInput);
$("#fake-search").val($("#fake-search").val() + changedInput);
// The line below will only start the timer if none exists;
to = to || setTimeout(function () {
doSomething(); // about html
}, 10000);
}
另一方面,如果您只想在他们完成输入后打开对话框
var to;
function dialogTrigger(event) {
var keyboardInput = event.which;
keyboardInput = keyboardInput + 5;
var changedInput = String.fromCharCode(keyboardInput);
$("#fake-search").val($("#fake-search").val() + changedInput);
// The line below clear any existing timeout and start a new one
if (to) clearTimeout(to);
to = setTimeout(function () {
doSomething(); // about html
}, 10000);
}
注意 10000ms 大约是 10 秒
我希望我的按键功能在每次按下时都能正常工作,但 setTimeout 不起作用。这是我的 html 和脚本;
html:
<div id="fake" class="full-height col-md-3 col-sm-12 col-xs-12">
<div class="nav-right">
<div class="form-search">
<input id="fake-search" onkeypress="dialogTrigger(event)" class="fake-search" type="text" />
</div>
</div>
</div>
脚本:
function dialogTrigger(event) {
var keyboardInput = event.which;
keyboardInput = keyboardInput + 5;
var changedInput = String.fromCharCode(keyboardInput);
$("#fake-search").val($("#fake-search").val() + changedInput);
setTimeout(function () {
doSomething(); // about html
}, 10000);
}
你需要决定逻辑是什么。如果这个想法是限制一个对话框在 10 秒后打开一次,那么你需要做这样的事情
var to;
function dialogTrigger(event) {
var keyboardInput = event.which;
keyboardInput = keyboardInput + 5;
var changedInput = String.fromCharCode(keyboardInput);
$("#fake-search").val($("#fake-search").val() + changedInput);
// The line below will only start the timer if none exists;
to = to || setTimeout(function () {
doSomething(); // about html
}, 10000);
}
另一方面,如果您只想在他们完成输入后打开对话框
var to;
function dialogTrigger(event) {
var keyboardInput = event.which;
keyboardInput = keyboardInput + 5;
var changedInput = String.fromCharCode(keyboardInput);
$("#fake-search").val($("#fake-search").val() + changedInput);
// The line below clear any existing timeout and start a new one
if (to) clearTimeout(to);
to = setTimeout(function () {
doSomething(); // about html
}, 10000);
}
注意 10000ms 大约是 10 秒