Skip to content

Commit

Permalink
New API: destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Aug 4, 2017
1 parent 4fcd453 commit 1e21471
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ <h2 id="options">Options</h2>
<div class="example">
<button class="btn" onclick="switchDPlayer()">Switch Video</button>
<button class="btn" onclick="dp2.notice('Notice演示')">Notice</button>
<button class="btn" onclick="dp2.destroy()">Destroy</button>
<div id="dplayer2"></div>
<script>
var dp2 = new DPlayer({
Expand Down
2 changes: 1 addition & 1 deletion dist/DPlayer.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/DPlayer.min.js.map

Large diffs are not rendered by default.

29 changes: 21 additions & 8 deletions src/DPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ class DPlayer {
}
this.pushDanmaku(danmakus);
}
window.requestAnimationFrame(this.animationFrame);
this.requestID = window.requestAnimationFrame(this.animationFrame);
};
window.requestAnimationFrame(this.animationFrame);
this.requestID = window.requestAnimationFrame(this.animationFrame);

this.setTime = (type) => {
if (!type) {
Expand Down Expand Up @@ -307,12 +307,12 @@ class DPlayer {
/**
* auto hide controller
*/
let hideTime = 0;
this.hideTime = 0;
if (!isMobile) {
const hideController = () => {
this.element.classList.remove('dplayer-hide-controller');
clearTimeout(hideTime);
hideTime = setTimeout(() => {
clearTimeout(this.hideTime);
this.hideTime = setTimeout(() => {
if (this.video.played.length) {
this.element.classList.add('dplayer-hide-controller');
closeSetting();
Expand Down Expand Up @@ -605,7 +605,7 @@ class DPlayer {
this.element.classList.add('dplayer-show-controller');

disableHide = setInterval(() => {
clearTimeout(hideTime);
clearTimeout(this.hideTime);
}, 1000);
commentFocusTimeout = setTimeout(() => {
commentInput.focus();
Expand Down Expand Up @@ -867,6 +867,7 @@ class DPlayer {
this.video.pause();
this.clearTime();
this.element.classList.remove('dplayer-playing');
window.cancelAnimationFrame(this.requestID);
this.trigger('pause');
}

Expand Down Expand Up @@ -1170,8 +1171,8 @@ class DPlayer {

// video download error: an error occurs
this.video.addEventListener('error', () => {
this.notice(this.tran('This video fails to load'), -1);
this.trigger('pause');
this.tran && this.notice && this.notice(this.tran('This video fails to load'), -1);
this.trigger && this.trigger('pause');
});

// video can play: enough data is available that the media can be played
Expand Down Expand Up @@ -1312,6 +1313,18 @@ class DPlayer {
noticeEle.style.opacity = 0;
}, time || 2000);
}

destroy () {
this.pause();
clearTimeout(this.hideTime);
this.video.src = '';
this.element.innerHTML = '';
for (const key in this) {
if (this.hasOwnProperty(key) && key !== 'paused') {
delete this[key];
}
}
}
}

module.exports = DPlayer;

0 comments on commit 1e21471

Please sign in to comment.