Javascript 检测用户首次键入和最后键入的时间,而不是介于两者之间

Javascript 检测用户首次键入和最后键入的时间,而不是介于两者之间,javascript,Javascript,我有这样一种情况,我只想在我第一次键入某个内容时启动一个函数,在我完成键入时启动另一个函数(空闲10秒) 我有这个: var keyPressElements = document.querySelectorAll('#waste-form input,#address,.container .form-control,.widget-box .form-control,#aria-main-search-form-field,#footer-search-field,#aria-feedbac

我有这样一种情况,我只想在我第一次键入某个内容时启动一个函数,在我完成键入时启动另一个函数(空闲10秒)

我有这个:

var keyPressElements = document.querySelectorAll('#waste-form input,#address,.container .form-control,.widget-box .form-control,#aria-main-search-form-field,#footer-search-field,#aria-feedback-form-field');
keyPressElements.forEach(function(elem) {
    elem.addEventListener('keypress', function() {
        updateLastTypedTime();
    });
});
    
function updateLastTypedTime() {
    if (searchTimeout != undefined)
        clearTimeout(searchTimeout);
    isUserTyping = true;
    console.log("Telling UpdateViewAPI that the user is still typing...");
    UpdateViewAPI();
    searchTimeout = setTimeout(callServerScript, 10000);
}

function callServerScript() {
        console.log("Telling UpdateViewAPI that the user hasn't typed in 10 seconds.");
        isUserTyping = false;
        UpdateViewAPI();
    }
但问题是,每次我键入时,它都会触发updateLastTypedTime()


谢谢

看起来您需要另一个函数,该函数仅在用户尚未键入时从
updateLastTypedTime
调用,例如:

function updateLastTypedTime() {
        if (searchTimeout != undefined)
            clearTimeout(searchTimeout);
        if (!isUserTyping)
            updateStartTyping();
        isUserTyping = true;
        searchTimeout = setTimeout(callServerScript, 10000);
    }
var-keyPressElements=document.queryselectoral(“测试输入”);
按键Elements.forEach(功能(元素){
元素addEventListener('keypress',function(){
updateLastTypedTime();
});
});
var搜索超时;
var isUserTyping=false;
函数updateLastTypedTime(){
if(searchTimeout!=未定义)
clearTimeout(搜索超时);
如果(!isUserTyping)
updateStartTyping();
isUserTyping=true;
searchTimeout=setTimeout(callServerScript,10000);
}
函数updateStartTyping(){
log(“用户开始键入”);
}
函数callServerScript(){
log(“用户停止键入”);
isUserTyping=false;
}

它应该调用updateLastTypedTime,因为每次按键时都会调用它。。。。里面的代码似乎在取消超时的地方执行您想要的操作。你似乎没有支票说这是第一次打字。它没有飞过我的头顶。。。你只需要添加一个检查,如果它是活动的,如果不是,你知道这是第一次
if(!isUserTyping){/*您知道这是第一次*/}