Triggering an event

This recent blog post says that this is how you go about Triggering events in plain JavaScript


 var elem = document.getElementById('myForm');
    var event = new Event('submit');
    elem.dispatchEvent(event);

Now why is this better than using addEventListener, or the .onsubmit property?

From what I can see of how it works you’d use that when you want to trigger the event without any intervention by the person visiting the page.

For example you could put that code in a function and trigger it using a setTimeout to submit the form after a specific time regardless of whether the person has started filling out the form or is part way through. A simple way to limit the amount of time allowed for entering info in the form because it gets submitted automatically by that code if the person hasn’t already triggered the listener or handler directly…