0

As the title states, I'm seeking to know if there's an event that is fired on any/all user activity?

I'm looking to do something like this

<body onanyevent="someFunc()">
...
</body>

someFunc would run whenever a user does anything on the body of the web page.

prismo
  • 1,505
  • 2
  • 12
  • 26
  • Can you clarify why you need this? There is an easy way to listen with jquery for multiple events. https://stackoverflow.com/questions/2534089/jquery-multiple-events-to-trigger-the-same-function (if you think about it, there really isn't much a user can do on a website without moving the mouse or using the keyboard, so listen to any mousemove or keyup/focus on forms is pretty much ALL activity besides staring at the screen) – rx2347 Mar 06 '20 at 19:52
  • Sure. I'm calling a function using setInterval and whenever the user does something, I want to clear that interval and restart. Is that sufficient? – prismo Mar 06 '20 at 19:54
  • What do you mean by anytime a user does something? Is moving a mouse something? Is clicking on the page something? How about if the element isn’t interactive? Right clicks? Is hovering a specific element something? Is unhovering something? Are keystrokes something? What if they’re not in a field of any kind? Tell exactly what you’re really trying to solve with this, rather than just how to implement the solution you’ve contrived, and you’ll get better answers – bryan60 Mar 06 '20 at 20:34

3 Answers3

0

If you want a event whenever a customer clicked an button of the homepage you can use the following code (for example):

const button1 = document.getElementById('button1');
button1.addEventListener('click', clickedButton);

function clickedButton() {
//do something

}

0

There is no single event that is fired for any user interaction.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
0

I'll add my approach to the answers, so it doesn't get lost in the comments:

Short answer: No, there is no single event for user activity.

Long answer:

I think the approach here is to define a number of events which are considered "user activity" like mousemove, keyup or focus/blur on formfields and then set up a listener to these events. This can be easily done in jQuery like already explained here:

jQuery multiple events to trigger the same function

Depending on the actual page (which we dont know anything about), there could be more events besides mousemove, keyups, focus/blur on all formfields that are considered user-activity. Closing a popup for example.

To listen to more events, check https://api.jquery.com/category/events/

Hope this helps.

rx2347
  • 1,071
  • 1
  • 6
  • 26