Reputation: 241
I am writing a program that as an alternate form of authentication uses keystroke rhythm (non-biometric). It records the keystroke time when the user pushes a keydown, and when he releases. However, while the key is held (due to the nature of the onkeydown function in Javascript), the function recording the time is called over and over again while holding the key. Is there a way to avoid this nuisance? Important Code Below, whole project (IDK if it would help): https://bitbucket.org/niklam/krauths-v.-1.0
<!DOCTYPE html>
<html>
<body>
<h1> KRAUTHS Account Creator</h1>
<link type="text/css" rel="stylesheet" href="style.css"/>
<input id="a" onkeydown="start()" onkeyup="start()"> <br>
<br>
<br>
<button onclick="submit()"> Submit</button>
<script>
var x = 0;
var timer = 0;
var list = ""
function run(){
timer = timer + 1;
setTimeout(run, 1);
}
function start(){
x = x + 1;
if(x==1){
run();
}
else{
list = list + timer +",";
timer = 0;
}
}
function submit(){
// ignore my http request, not important
list = list.substring(0,list.length-1);
alert(list);
var request = new XMLHttpRequest();
request.open("GET","../add?u="+prompt("Enter your username")+"&n="+list+"&l=m",false);
request.onreadystatechange = function(){
if(request.status == 200 && request.readyState == 4){
alert(request.responseText);
}
}
request.send();
}
</script>
</body>
</html>
Upvotes: 1
Views: 46
Reputation: 16214
The straight way - set a flag on first keydown and unset it on keyup, ignore input when it is set.
Upvotes: 3
Reputation: 1276
You could use the _debounce function in http://underscorejs.org/. It will only take the last key press after the user lifts his key. Just set a delay on when you want to register the keystrokes. There are also other functions that could apply to you such as throttle.
Upvotes: 0