Skip to content

Commit a7dacdf

Browse files
committed
replace on change with on input event listener
1 parent 62381a7 commit a7dacdf

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

examples/simple-player/README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,17 @@ the linting is now done via the npm run lint command of the main project
8484

8585
## notes about problems I encountered during development
8686

87-
### using es6 modules with nodejs
87+
### client problems
88+
89+
#### input type range not triggering change event
90+
91+
Because the value of the input type range element gets constantly updated by the player itself, when using an event listener on **change** will result in the event not firing at all (chrome only?) or it will fire but then at the moment when you read the value it will already have been updated by the player
92+
93+
this is why I recommend using the **input** event instead (which is what is used in this example), so far I had none of the problems I just mentioned using the "input" event instead of the "change" event
94+
95+
### server problems
96+
97+
#### using es6 modules with nodejs
8898

8999
Full nodejs support for es6 modules import/export syntax enabled
90100

@@ -109,7 +119,7 @@ and in package.json you will notice that I also added:
109119
}
110120
```
111121

112-
### replacing cjs dirname with esm import.meta.url
122+
#### replacing cjs dirname with esm import.meta.url
113123

114124
When using es modules (esm), the __dirname environment variable is not available anymore, as it is for commonjs (cjs) only, however a replacement is available: meta.import.url
115125

examples/simple-player/client/src/bootstrap.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { PlayerUI } from './library/player/ui.js'
1818
// check out the documentation part of the readme
1919
const options: ICoreOptions = {
2020
soundsBaseUrl: 'https://mp3l.jamendo.com/?trackid=',
21-
playingProgressIntervalTime: 500,
2221
loadPlayerMode: PlayerCore.PLAYER_MODE_AUDIO,
2322
}
2423

examples/simple-player/client/src/library/player/ui.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class PlayerUI {
4444
protected _createListeners(): void {
4545

4646
this._buttonsBox.addEventListener('click', this._onClickButtonsBox.bind(this))
47-
this._volumeSlider.addEventListener('change', this._onChangeVolume.bind(this))
48-
this._playingProgressBar.addEventListener('change', this._onChangePlayingProgress.bind(this))
47+
this._volumeSlider.addEventListener('input', this._onInputVolume.bind(this))
48+
this._playingProgressBar.addEventListener('input', this._onInputPlayingProgress.bind(this))
4949

5050
}
5151

@@ -132,7 +132,12 @@ class PlayerUI {
132132

133133
}
134134

135-
protected _onChangeVolume(event: Event): void {
135+
protected _onInputVolume(event: Event): void {
136+
137+
// Note: for input type=range elements listen for input events
138+
// instead of change events, the change event will sometimes
139+
// not fire if the click on the element happens at the same
140+
// exact moment at which the user clicks on the input element
136141

137142
// styling the html5 range:
138143
// http://brennaobrien.com/blog/2014/05/style-input-type-range-in-every-browser.html
@@ -144,11 +149,9 @@ class PlayerUI {
144149

145150
}
146151

147-
protected _onChangePlayingProgress(event: Event): void {
148-
152+
protected _onInputPlayingProgress(event: Event): void {
149153
const rangeElement = event.target as HTMLInputElement
150154
const value = parseInt(rangeElement.value)
151-
152155
this.player.setPosition(value)
153156

154157
}
@@ -211,8 +214,8 @@ class PlayerUI {
211214
protected _destroyListeners(): void {
212215

213216
this._buttonsBox.removeEventListener('click', this._onClickButtonsBox.bind(this))
214-
this._volumeSlider.removeEventListener('change', this._onChangeVolume.bind(this))
215-
this._playingProgressBar.removeEventListener('click', this._onChangePlayingProgress.bind(this))
217+
this._volumeSlider.removeEventListener('change', this._onInputVolume.bind(this))
218+
this._playingProgressBar.removeEventListener('click', this._onInputPlayingProgress.bind(this))
216219

217220
this._buttonsBox = null
218221
this._volumeSlider = null

0 commit comments

Comments
 (0)