Reputation: 1767
I'm trying to make the following code work:
document.onmousedown = function(e){
setTimeout(function(){ e }, 3000);
};
This is just pseudo code, but I want to capture the mousedown event and fire it later. It doesn't matter if it fires the first time or not (ie preventdefault).
If this was a click event I know I could just save the clientX, clientY of e and reinitialize the event, but I couldn't find any way to simulate mouse down with JavaScript.
Upvotes: 2
Views: 2935
Reputation: 12
Your code has no problem. Maybe somewhere else replace the onmousedown listenter. It's best you use addEventListener
document.addEventListener("mousedown", function(e) {
setTimeout(function() {
//your code here
}, 3000)});
or if you are using jQuery, you can use this code
$(document).on("mousedown", function(e) {
setTimeout(function() {
//your code here
}, 3000);
});
Upvotes: 0
Reputation: 18525
You can try something like this:
document.onmousedown = (args) =>
setTimeout(() => fn.apply(null, [args]), 500)
var fn = (args) => {
console.log('onmousedown args', args)
}
Click anywhere and check the console (wait for it)
If I understand you correctly this will allow you to execute that onmousedown
event at a later time with the same params.
See it here
Upvotes: 1
Reputation: 1063
If you want to trigger a mousedown event, just use this api MouseEvent()
The MouseEvent() constructor creates a new MouseEvent.
The code is like this:
var trigger = false
document.onmousedown = function(e){
var t = new MouseEvent('mousedown',e)
console.log(t)
if(!trigger)
{
trigger = true;
document.dispatchEvent(t)
}
};
Upvotes: 3
Reputation: 1658
Try this code
HTML Code:
<p id="myParaid">
Click the text! The mouseDown() function is triggered when the mouse button is pressed down over this paragraph,
</p>
JS Code:
$( "#myParaid" ).mousedown(function() {
alert( "Handler for .mousedown() called." );
setTimeout(myFunction, 3000);
});
function myFunction(){
alert('3000 Over');
}
Upvotes: 1