This is a jQuery time update events for a video player with multiple id
s. I feel the code is too long and I need to add more id
s to fire events.
$(document).ready(function() {
var i = 0;
var a = 0;
var b = 0;
var c = 0;
var j = 0;
$("#r-news-week1, #r-news-week2").bind("play", function() {
if (i < 1) {
console.log("Video Started");
ga('send', 'event', 'Videos', 'Started', 'Video');
}
i = i + 1;
});
$("#r-news-week1, #r-news-week2").bind("timeupdate", function() {
var currentTime = this.currentTime;
if (currentTime > 0.75 * (this.duration)) {
if (c < 1) {
console.log("watched 75%");
ga('send', 'event', 'Videos', 'Watched 75%', 'Video');
} c = c + 1;
} else if (currentTime > 0.50 * (this.duration)) {
if (b < 1) {
console.log("watched 50%");
ga('send', 'event', 'Videos', 'Watched 50%', 'Video');
} b = b + 1;
} else if (currentTime > 0.25 * (this.duration)) {
if (a < 1) {
console.log("watched 25%");
ga('send', 'event', 'Videos', 'Watched 25%', 'Video');
} a = a + 1;
}
});
$("#r-news-week1, #r-news-week2").bind("ended", function() {
if (j < 1) {
console.log("Finished 100%");
ga('send', 'event', 'Videos', 'Finished 100%', 'Video');
}
j = j + 1;
});
})
i
,a
, ...bind
is deprecated, useon
. Cache$("#r-news-week1, #r-news-week2")
, Chain methods on same object. Forplay
andended
, theif
conditions are not required as these events will fire only once, you can useone()
to bind such events. \$\endgroup\$