简体   繁体   English

jQuery检查不是我想要的方式

[英]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秒钟之前已更改:

  • if yes, means that the user is typing, so it's useless to check the answer, we wait he's finished to type his answer 如果是,则表示用户正在输入,因此检查答案无用,我们等待他完成输入答案
  • if no (hasn't changed), means the user isn't typing anything since 1 second, then you can launch the check... 如果没有(未更改),则表示用户自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 : 另一种(或补充)策略是说:

  • if Keydown, then the user starting typing, so we stop the setTimeout by a clearTimeout 如果是Keydown,则用户开始输入内容,因此我们以clearTimeout停止setTimeout
  • if KeyUp, then the user has finished typing, so we start the setTimeout... 如果是KeyUp,则用户已完成输入,因此我们启动了setTimeout ...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM