Question

I am trying to create a Virtual Keyboard using Javascript and an SVG keyboard image. I have been able to access the onmousedown property of a 'button' element and make a call to myFunction when that button is pressed.

button.onmousedown = (function(self) {
    return function() { self.myFunction(); };
})(this);

I was hoping to add a repeat key feature and want to incorporate setInterval so that myFunction repeats every second or so. I am having trouble incorporating the setInterval into the onmousedown functionality. Should I try to add setInterval to above code or add it to myFunction? I want to use just basic Javascript. Thanks in advance.

Was it helpful?

Solution

var interval;

button.addEventListener('mousedown', function() {
  interval = setInterval(doSomething, 20);
});

button.addEventListener('mouseup', function () {
  clearInterval(interval);
});

function doSomething()
{
  //your code here
}

Something like this?

Live example: http://cssdeck.com/labs/cbpb6qqm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top