I was trying to write a script to 'play' some keys on this online keyboard by using JavaScript to 'click' the keys for me.
The code
//sample array to iterate over
var keys_ = ['et', 'dst', 'et', 'dst', 'et', 'b', 'dt', 'ct', 'a', ...];
//handles the clicking only
function playKey(id_) {
key_ = document.getElementById(id_);
key_.click(); }
//iterates over the array
function playKeys(keys_) {
delay = 1000;
for (i = 0; i < keys_.length; i++) {
console.log(delay);
key_ = keys_[i];
console.log(key_);
window.setTimeout('playKey(key_)', delay);
delay += 1000;
}
}
The output
The console throws the following error:
1000
et
2000
dst
...
9000
a
undefined
Uncaught TypeError: Cannot read property 'style' of null p-ano.html:142
8 Uncaught TypeError: Cannot call method 'click' of null
As you can see, the delay
and key_
values are perfectly correct. But still when I execute this, after a second (i.e., the 1st timeout), all the keys seem to play at once and then nothing happens.
What am I doing wrong?
P.S.: I've seen other questions like this one and searched Google and other forums, to no avail