Does jQuery .trigger() or .triggerHandler() work across two separate JavaScript files? I have a timeline that when interacted with, triggers a function
scrubbingToTime(position). In a separate JavaScript file, I want to detect when scrubbingToTime(position) is being called, so I was looking into jQuery .trigger(). The code is hard to reproduce, but the idea was that it was like:
timeline.js
function scrubbingToTime(position) {
$(document).trigger("scrub", [position]);
}
catch.js
$(document).on("scrub", function(e, pos) {
console.log("Time Changed!");
});
This does not work, however if the trigger and on are in the same file, it works fine. For example,
timeline.js
function scrubbingToTime(position) {
$(document).trigger("scrub", [position]);
$(document).on("scrub", function(e, pos) {
console.log("Time Changed!");
});
}
This actually console.logs the Time Changed! message. Is there any reason why the first (what I want) won't work?
You need to make sure the order of the JS loading is correct.
If you are loading your catch.js file first, then timeline.js won't know about scrub because it hasn't been loaded yet.