Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add failsafe to get duration during timeupdate #20

Merged
merged 1 commit into from
Aug 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/tracking/percentile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,42 @@ const PercentileTracking = function(config) {
let first = false;
let second = false;
let third = false;
let duration = 0;
let duration = null;
let pauseCount = 0;
let seekCount = 0;

const reset = function(e) {
first = false;
second = false;
third = false;
duration = 0;
duration = null;
pauseCount = 0;
seekCount = 0;
};

const incPause = () => pauseCount++;
const incSeek = () => seekCount++;

const getDuration = function() {
duration = +player.duration().toFixed(0);
if (duration > 0) {
const quarter = (duration / 4).toFixed(0);

first = +quarter;
second = +quarter * 2;
third = +quarter * 3;
}
};

player.on('dispose', reset);
player.on('loadstart', reset);
player.on('tracking:pause', incPause);
player.on('tracking:seek', incSeek);
player.on('timeupdate', function() {
if (duration === null) {
getDuration();
}

const curTime = +player.currentTime().toFixed(0);
const data = {
seekCount,
Expand Down Expand Up @@ -80,16 +95,7 @@ const PercentileTracking = function(config) {
player.trigger('tracking:fourth-quarter', data);
});

player.on('durationchange', function() {
duration = +player.duration().toFixed(0);
if (duration > 0) {
const quarter = (duration / 4).toFixed(0);

first = +quarter;
second = +quarter * 2;
third = +quarter * 3;
}
});
player.on('durationchange', getDuration);
};

export default PercentileTracking;