0

I want to make a JavaScript program simulate a mouse click wherever the mouse is on a timed interval. I know of the

if(mouseIsPressed) 

and the

if(mouseDown) 

commands, but are there any commands that make my mouse automatically click, some kind of forceMouseDown command maybe?

14
  • If you want to do something like cookie clicker, you should use timeout instead. I mean, if you want to run a function every n seconds, you should set a timeout. Commented Feb 1, 2017 at 14:57
  • Ok, I will do some research on timeout and if I can solve my problem I will answer my own question. Commented Feb 1, 2017 at 14:59
  • It depends, what do you want to do ? Commented Feb 1, 2017 at 14:59
  • developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click You can simulate a click event on any element you can select. If there's no html involved, just trigger the click handler manually. Commented Feb 1, 2017 at 14:59
  • What do you mean, I'm kinda new to this. Commented Feb 1, 2017 at 15:00

2 Answers 2

2

If you just want to click a button, as I see from the comment, than just click the specific button with HTMLElement.click() in an interval.

Like this:

var myButton = document.getElementById('my-button');

// Just for example
var clickCount = 0;
var clickStatus = document.getElementById('clicks');


setInterval(function(){
  myButton.click();
  clickStatus.innerText = ++clickCount;
}, 2000)
<button id="my-button">My Button</button>
<p>Clicks: <span id="clicks">0</span></p>

Sign up to request clarification or add additional context in comments.

6 Comments

So do you think that this would work in a canvas to simulate a click for a rectangle?
Also, is the part that times the click located here clickStatus.innerText = ++clickCount; }, 2000) in the 2000 part?
Sure just change the myButton to point to the element inside the Canvas. As long as it's an HTML Element it should work just fine
The 2000 is the number of milliseconds to wait between each interval == 2 seconds..
Thanks, that's what I thought but I thought I should check with you first.
|
1

Can use elementFromPoint() to identify top most element at current mouse position.

Combine that with a mousemove listener to track mouse position in page

var mousePos ={x:0,y:0}


setInterval(function(){  
  document.elementFromPoint(mousePos.x, mousePos.y).click()
}, 2000)

document.addEventListener('mousemove', function(e){
  mousePos.x = e.clientX;
  mousePos.y = e.clientY;  
});

6 Comments

May I ask what the e.clientX; and e.client does or simulates?
it is the pixel location within the document of mouse on x axis
what is mouseX or mouseY?
Well thank you guys for this, I will test it and be back later to give feedback on how it works.
You can't locate mouse if you don't track it's movement
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.