admin管理员组文章数量:1352138
I am learning javascript and at the same time trying to create a simple script that allows you to type a foreign language on a web browser with your keyboard.
So, when you type a
for example, there is a single letter mapped to a, so a single character Դ
would appear, however to make a character like 厄
appear, you have to type twice, since this language has more characters than the English alphabet.
So, the problem is with that last character. Normally, I should have to type g
and h
consecutively in the span of one second to produce the 厄
letter, but I have problems waiting to check if within two characters have been typed within one second of eachother inorder to show that letter.
So, I don't think time interval functions are the way to go about this, but I can't see any other method also.
I am learning javascript and at the same time trying to create a simple script that allows you to type a foreign language on a web browser with your keyboard.
So, when you type a
for example, there is a single letter mapped to a, so a single character Դ
would appear, however to make a character like 厄
appear, you have to type twice, since this language has more characters than the English alphabet.
So, the problem is with that last character. Normally, I should have to type g
and h
consecutively in the span of one second to produce the 厄
letter, but I have problems waiting to check if within two characters have been typed within one second of eachother inorder to show that letter.
So, I don't think time interval functions are the way to go about this, but I can't see any other method also.
Share Improve this question edited Feb 28, 2017 at 14:29 ʎɹnɔɹǝW asked Feb 28, 2017 at 14:25 ʎɹnɔɹǝWʎɹnɔɹǝW 8912 gold badges8 silver badges15 bronze badges 8- 2 Store the last pressed time and measure the difference between it and now for every subsequent key pressed. – alex Commented Feb 28, 2017 at 14:27
- Have them hold down another key such as CTRL when two characters need to be typed consecutively – Dexygen Commented Feb 28, 2017 at 14:29
- @alex I think that's a good idea, I don't know if I can divide the timing a get the response I need. It could e up as a float and would be hard to know which is a two-key-press letter or not – ʎɹnɔɹǝW Commented Feb 28, 2017 at 14:31
- @GeorgeJempty Yeah, that would work if there were few of those two-key-press letter, unfortunatelly, they are like ~50 so it would be cumbersome to use it – ʎɹnɔɹǝW Commented Feb 28, 2017 at 14:33
- 1 Yes but measuring the time between key-presses is guesswork. Better cumbersome and precise in my opinion – Dexygen Commented Feb 28, 2017 at 14:35
4 Answers
Reset to default 3cautions: check key whether up in keydown() & notify keydown the key is upped in keypress();
var start = null;
$('#in').keydown(function (e) {
if (!start) {//checking is a new user input
start = $.now();
}
}).keyup(function (e) {
var timeElapsed = $.now() - start;
start = null;//start the next timing tracking
console.log(['time elapsed:', timeElapsed, 'ms'].join(' '));
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<h2>please input some letter to test</h2>
<input id="in"/>
</label>
another answer about your question,perhaps this is your truly answer.
function duration(timestamps) {
var last = timestamps.pop();
var durations = [];
while (timestamps.length) {
durations.push(last - (last = timestamps.pop()));
}
return durations.reverse();
}
function display(mills) {
if (mills > 1000)
return (mills / 1000) + ' s';
return mills + ' ms';
}
var durations = [];
$('#in').keydown(function (e) {
durations.push($.now());
}).keyup(function (e) {
var current = durations;
current.push($.now());
durations = [];
var timeElapsed = current[current.length - 1] - current[0];
console.log([
['time elapsed:', display(timeElapsed)].join(' '),
['keys duration:', duration(current).map(display)].join(' ')
].join(' --- '));
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<h2>Please input something to test!</h2>
<input id="in"/>
</label>
Below is the sample code to measure the elapsed time between any 2 events.
I added setTimeout
just to give you an example.
var startTime = Date.now();
setTimeout(function(){
var elapsedTime = Date.now() - startTime;
console.log('elapsedTime ='+elapsedTime);
}, 100);
Like Alex mentioned, for every press, simply store new Date().getTime();
in a variable, which will get you the latest UTC time. UTC time is given in milliseconds, so on the next key-press, just see if the current time and the stored time differ by 1000!
本文标签: Detect time lapse between two key press in JavascriptStack Overflow
版权声明:本文标题:Detect time lapse between two key press in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743891500a2557002.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论