mediaevents
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -11,11 +11,3 @@ /* mediaEvents | ||
class mediaEvents { | ||
constructor (timings, type, textNode) { | ||
if (type !== null) { | ||
this.type = type | ||
} else { | ||
this.type = 'events' | ||
} | ||
if (type === 'text' && textNode === null) console.error('You need to add a DOMNode for the "text" event to attach to') | ||
this.textNode = textNode | ||
constructor (timings) { | ||
if (timings === null) console.error('You need to specify a timings object') | ||
@@ -30,3 +22,3 @@ this.timings = timings | ||
getActiveTimingValue (currentTime) { | ||
getActiveTimingKey (currentTime) { | ||
const timingKeys = Object.keys(this.timings) | ||
@@ -40,13 +32,9 @@ | ||
return this.timings[activeTimings[activeTimings.length - 1]] | ||
return activeTimings[activeTimings.length - 1] | ||
} | ||
executeEvent (timingKey) { | ||
window[timingKey]() | ||
this.timings[timingKey]() | ||
} | ||
setText (timingKey) { | ||
this.textNode.innerHTML = timingKey | ||
} | ||
isCache (event, timingKey) { | ||
@@ -67,12 +55,6 @@ if (this.cache[event] == timingKey) return true | ||
mediaNode.addEventListener(eventListener, () => { | ||
let timingKey = this.getActiveTimingValue(mediaNode.currentTime) | ||
let timingKey = this.getActiveTimingKey(mediaNode.currentTime) | ||
if (timingKey !== null && !this.isCache(eventListener, timingKey)) { | ||
if (this.type == 'text') { | ||
this.setText(timingKey) | ||
} | ||
else if (this.type == 'events') { | ||
this.executeEvent(timingKey) | ||
} | ||
this.executeEvent(timingKey) | ||
this.setCache(eventListener, timingKey) | ||
@@ -79,0 +61,0 @@ } |
{ | ||
"name": "mediaevents", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Fire events for specific timeframes easily", | ||
@@ -5,0 +5,0 @@ "main": "mediaevents.js", |
@@ -15,3 +15,3 @@ # mediaEvents | ||
```html | ||
<script src="https://unpkg.com/mediaevents@1.0.0/mediaevents.js"></script> | ||
<script src="https://unpkg.com/mediaevents@1.0.1/mediaevents.js"></script> | ||
``` | ||
@@ -30,29 +30,11 @@ | ||
const timings = { | ||
"0": "Introduction", | ||
"10": "This text shows up after 10 seconds", | ||
"15": "And this one after 15" | ||
"0": function() { | ||
text.innerHTML = 'New text at start' | ||
}, | ||
"10": function() { | ||
document.body.style.backgroundColor = 'lightblue' | ||
text.innerHTML = 'New text and background color after 10 seconds' | ||
} | ||
} | ||
const ev = new mediaEvents(timings, 'text', text) | ||
ev.bind(media) | ||
``` | ||
**Event:** | ||
```js | ||
const media = document.getElementById('audioplayer') | ||
const timings = { | ||
"0": "handleIntro", | ||
"10": "handleTen" | ||
} | ||
function handleIntro () { | ||
console.log("This executes at 0 seconds") | ||
} | ||
function handleTen () { | ||
console.log("This text shows up after 10 seconds") | ||
} | ||
const ev = new mediaEvents(timings) | ||
@@ -64,7 +46,5 @@ ev.bind(media) | ||
### `new mediaEvents(timings, type, textNode)` | ||
### `new mediaEvents(timings)` | ||
* **`timings`:** (required) Object where the keys represent the time and the values either are text or function names (accessible on `window`) | ||
* **`type`:** (optional) Type of event handling, can be either `text` or `events` (`events` by default) | ||
* **`textNode`:** (optional) If the `text` event type is used, this is required. Reference of a DOMNode where the text should be put in. | ||
* **`timings`:** (required) Object where the keys represent the time and the values are functions or function references | ||
@@ -71,0 +51,0 @@ ### `mediaEvents.bind(mediaNode)` |
Sorry, the diff of this file is not supported yet
4862
4
52
60