[英]Jquery check is not behaving the way I would like to
So, I am creating a check with javascript/jquery, when a user fills out something it should check whether the answer is right or wrong. 因此,我正在使用javascript / jquery创建检查,当用户填写内容时,应该检查答案是对还是错。 But whenever I try to do so it will say with each letter I fill out it's wrong untill I reach the correct answer. 但是,每当我尝试这样做时,它都会在我填写的每个字母上都说不对,直到我得到正确的答案。 For instance we take the word "example". 例如,我们使用“示例”一词。 You as user should write "example", but when u are writing example it checks them as wrong untill you have written the word "example". 您作为用户应该写“ example”,但是当您编写示例时,它将检查它们是否错误,直到您写了“ example”一词。 How can I prevent such thing from happening? 如何防止此类情况发生? For it to evaluate the word after 1 or 1.5 seconds? 要在1或1.5秒后评估单词? This is my code: 这是我的代码:
function prepareCheck() {
$.getJSON('json_files/jsonData_' + ID + '.json', function(json) {
$(document).on('keyup', '.syl-input', function() {
var rowCounter = $(this).parent().parent().parent().parent().attr('id');
var inputCounter = $(this).attr('id');
var jsyl = json.main_object.main_object.exercises[rowCounter].syllables[inputCounter];
var jsylall = json.main_object.main_object.exercises[rowCounter].syllables;
var valueInput = $(this).val();
console.log(jsyl);
if (valueInput == jsyl) {
S.addRight();
} else if ($.inArray(valueInput, jsylall) >= -1) {
S.addWrong();
}
});
});
}
You can use code such 您可以使用这样的代码
setInterval(check_answer(), 1000);
for starting the check 开始检查
And check_answer() checks if the answer has changed since 1 second before : 然后check_answer()检查答案是否在1秒钟之前已更改:
So code can be like this : 所以代码可以像这样:
setInterval(check_answer(), 1000);
var old_answer="";
function check_answer() {
new_answer = $("#answer").val();
if ( new_answer==old_answer) {
// nothing typed since 1 second, i launch the control and display error message if answer invalid...
}
}
Another (or complementary) strategy is to say that : 另一种(或补充)策略是说:
Sample code : 样例代码:
var timer;
$field_to_check.on('keyup', function () {
clearTimeout(timer);
timer = setTimeout(check_answer, 1000);
});
//on keydown, clear the countdown
$field_to_check.on('keydown', function () {
clearTimeout(timer);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.