How do you track each setInterval cycle?
function count(){
//user input stored in the last index of the array each cycle
array[lastindex]=userinput;
setInterval(count,5000);
}
Like me sample codes above, I am populating the array by storing the user inputs.However, I want to add one more case to this, which is if the users dont type in anything within 5 seconds, I am still able to store a "mark" that indicates that. in other words, how can I detect this automatically before t开发者_StackOverflow中文版he next cycle? Thank you
I imagine it like this - store a variable that indicates that user is typing now. Add an event, for example, keyup to the input where user types in, that after 5 sec of idleing changes that varianble.
var
userTyping = false,
typingId;
$("input").keyup(function(){
userTyping = true;
window.clearInterval(typingId);
typingId = window.setTimeout(function(){userTyping = false;}, 5000);
});
And then the check:
function count(){
//user input stored in the last index of the array each cycle
if(!userTyping){
array[lastindex]=userinput;
}
setTimeout(count,5000);
}
Note that you should use setTimeout if within the function, not the setInterval each time you call - that would end up badly...
So first question is does no new input means userInputOld === userInput
or userInput === ""
? I'll guess no, because than it would be trivial to check.
You must make a variable that will mark whether there was input or not. Let's presume keyboard input:
var hasInput = false;
$("input").keyup(function(){hasInput = true;})
Then you can check it in your code
function count(){
//user input stored in the last index of the array each cycle
if (hasInput) {
array[lastindex]=userinput;
} else {
aray[lastindex]=null;
}
setInterval(count,5000);
}
Anyway I would like to ask what exactly are you doing, because it looks like you can probably do it better altogether.
精彩评论