'Executing a JavaScript with an event that will only occur when any other event occurs

I tried to check a HTML DOM Events' document to see if there's a HTML DOM Event that will only occur when any other event without a specific event occurs, but I found nothing..

Is there a HTML DOM Event that will only occur when any other event occurs?

If no, is it possible to create a JavaScript that will only execute if any event occurs?
(I mean, no specific event, it will always execute if any event occurs.)

If it's possible, could you please show me an idea how to create it?



Solution 1:[1]

If I understand you correctly, you want to react to changes in the DOM, that are not made by your script.

You should look into the MutationObserver-API if you are not trying to support old browsers. Taking the example there as a starting point, you could try something like this:

// select the target node
var target = document.querySelector('body');

function mutationCallback(mutation) {
  /*
    Your logic here
  */
}

// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(mutationCallback);    
});

// configuration of the observer:
var config = { attributes: true, childList: true };

// pass in the target node, as well as the observer options
observer.observe(target, config);

I hope this helps.

Solution 2:[2]

No, there isn't an event that triggers when any event occurs: it would probably not have a use other than crashing your browser. If you're trying to see if the user is away from his keyboard, then there are a lot of other methods, as asking him so, and seeing if he responds in a certain range of time. If you don't want him to notice that you're doing an afk test, then use the mousemove event listener on the body element

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sebastian G. Marinescu
Solution 2 towc