Handling/touching/processing MIDI events with JavaScript in Max will mess up the timing due to how Max handles scheduling — it will defer the event to a lower priority thread — so I don't recommend doing that. You can safely use JS for GUI code and using Ableton's API to manipulate the session and UI, though.