Bestanden laden laat site crashen
Voor een spelletje gebruik ik een Loader object om bestanden in te laden (in dit geval 648 geluidsbestandjes).
Maar omdat er zoveel ingeladen moet worden crasht de website en loopt de browser vast (als het te lang duurt). Hoe los ik dit op?
Dit is het Loader object:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* @class Loader
* @param {Client} parent
*/
var Loader = function Loader(parent)
{
/**
* @returns {Array}
*/
this._imageDataList;
/**
* @returns {Uint}
*/
this._imagesLoaded;
/**
* @returns {Array}
*/
this._soundDataList;
/**
* @returns {Uint}
*/
this._soundsLoaded;
/**
* @returns {Client}
*/
this.parent;
/**
* @constructor
*/
{
/* Initialize image data list */
this._imageDataList = [];
/* Initialize images loaded */
this._imagesLoaded = 0;
/* Initialize sound data list */
this._soundDataList = [];
/* Initialize sounds loaded */
this._soundsLoaded = 0;
/* Initialize parent */
this.parent = parent;
};
};
/**
* @method _loadImage
* @syntax Loader._loadImage(imageData)
* @param {Object} imageData
* @returns {Loaded}
* @private
*/
Loader.prototype._loadImage = function(imageData)
{
/* Initialize self */
var self = this;
/* Create image */
var image = jQuery('<img>');
/* Set source */
image.attr('src', imageData.path + imageData.name + '.' + imageData.extension);
/* Set data path */
image.attr('data-path', imageData.path);
/* Set data name */
image.attr('data-name', imageData.name);
/* Set data extension */
image.attr('data-extension', imageData.extension);
/* Add event listener */
image.one('load', function()
{
/* Increase images loaded */
self._imagesLoaded++;
/* Add asset */
self.parent.assets.addImage(jQuery(this));
});
/* Return this */
return this;
};
/**
* @method _loadSound
* @syntax Loader._loadSound(soundData)
* @param {Object} soundData
* @returns {Loaded}
* @private
*/
Loader.prototype._loadSound = function(soundData)
{
/* Initialize self */
var self = this;
/* Create sound */
var sound = jQuery('<audio>');
/* Loop trough list of extensions */
for (var i = soundData.extensions.length - 1; i >= 0; i--)
{
/* Create source */
var source = jQuery('<source>');
/* Set source */
source.attr('src', soundData.path + soundData.name + '.' + soundData.extensions[i]);
/* Set data extension */
source.attr('data-extension', soundData.extensions[i]);
/* Append source to sound */
sound.append(source);
}
/* Set data path */
sound.attr('data-path', soundData.path);
/* Set data name */
sound.attr('data-name', soundData.name);
/* Add event listener */
sound.one('canplaythrough', function()
{
/* Increase sounds loaded */
self._soundsLoaded++;
/* Add asset */
self.parent.assets.addSound(jQuery(this));
});
/* Return this */
return this;
};
/**
* @method _loadImages
* @syntax Loader._loadImages()
* @returns {Loader}
* @private
*/
Loader.prototype._loadImages = function()
{
/* Loop trough image data list */
for (var i = this._imageDataList.length - 1; i >= 0; i--)
{
/* Load image */
this._loadImage(this._imageDataList[i]);
}
/* Return this */
return this;
};
/**
* @method _loadSounds
* @syntax Loader._loadSounds()
* @returns {Loader}
* @private
*/
Loader.prototype._loadSounds = function()
{
/* Loop trough sound data list */
for (var i = this._soundDataList.length - 1; i >= 0; i--)
{
/* Load sound */
this._loadSound(this._soundDataList[i]);
}
/* Return this */
return this;
};
/**
* @method addImage
* @syntax Loader.addImage(path, name, extension)
* @param {String} path
* @param {String} name
* @param {String} extension
* @returns {Loaded}
* @public
*/
Loader.prototype.addImage = function(path, name, extension)
{
/* Add image data to image data list */
this._imageDataList.push(
{
path: path,
name: name,
extension: extension
});
/* Return this */
return this;
};
/**
* @method addSound
* @syntax Loader.addSound(path, name, extension)
* @param {String} path
* @param {String} name
* @param {Array} extensions
* @returns {Loaded}
* @public
*/
Loader.prototype.addSound = function(path, name, extensions)
{
/* Add sound data to sound data list */
this._soundDataList.push(
{
path: path,
name: name,
extensions: extensions
});
/* Return this */
return this;
};
/**
* @method progress
* @syntax Loader.progress()
* @returns {Uint}
* @public
*/
Loader.prototype.progress = function()
{
/* Calculate total */
var total = (this._imageDataList.length + this._soundDataList.length);
/* Calculate total loaded */
var totalLoaded = this._imagesLoaded + this._soundsLoaded;
/* Return progress */
return ((100 / total) * totalLoaded).toFixed(2);
};
/**
* @method run
* @syntax Loader.run()
* @returns {Loader}
* @public
*/
Loader.prototype.run = function()
{
/* Load images */
this._loadImages();
/* Load sounds */
this._loadSounds();
/* Return this */
return this;
};
* @class Loader
* @param {Client} parent
*/
var Loader = function Loader(parent)
{
/**
* @returns {Array}
*/
this._imageDataList;
/**
* @returns {Uint}
*/
this._imagesLoaded;
/**
* @returns {Array}
*/
this._soundDataList;
/**
* @returns {Uint}
*/
this._soundsLoaded;
/**
* @returns {Client}
*/
this.parent;
/**
* @constructor
*/
{
/* Initialize image data list */
this._imageDataList = [];
/* Initialize images loaded */
this._imagesLoaded = 0;
/* Initialize sound data list */
this._soundDataList = [];
/* Initialize sounds loaded */
this._soundsLoaded = 0;
/* Initialize parent */
this.parent = parent;
};
};
/**
* @method _loadImage
* @syntax Loader._loadImage(imageData)
* @param {Object} imageData
* @returns {Loaded}
* @private
*/
Loader.prototype._loadImage = function(imageData)
{
/* Initialize self */
var self = this;
/* Create image */
var image = jQuery('<img>');
/* Set source */
image.attr('src', imageData.path + imageData.name + '.' + imageData.extension);
/* Set data path */
image.attr('data-path', imageData.path);
/* Set data name */
image.attr('data-name', imageData.name);
/* Set data extension */
image.attr('data-extension', imageData.extension);
/* Add event listener */
image.one('load', function()
{
/* Increase images loaded */
self._imagesLoaded++;
/* Add asset */
self.parent.assets.addImage(jQuery(this));
});
/* Return this */
return this;
};
/**
* @method _loadSound
* @syntax Loader._loadSound(soundData)
* @param {Object} soundData
* @returns {Loaded}
* @private
*/
Loader.prototype._loadSound = function(soundData)
{
/* Initialize self */
var self = this;
/* Create sound */
var sound = jQuery('<audio>');
/* Loop trough list of extensions */
for (var i = soundData.extensions.length - 1; i >= 0; i--)
{
/* Create source */
var source = jQuery('<source>');
/* Set source */
source.attr('src', soundData.path + soundData.name + '.' + soundData.extensions[i]);
/* Set data extension */
source.attr('data-extension', soundData.extensions[i]);
/* Append source to sound */
sound.append(source);
}
/* Set data path */
sound.attr('data-path', soundData.path);
/* Set data name */
sound.attr('data-name', soundData.name);
/* Add event listener */
sound.one('canplaythrough', function()
{
/* Increase sounds loaded */
self._soundsLoaded++;
/* Add asset */
self.parent.assets.addSound(jQuery(this));
});
/* Return this */
return this;
};
/**
* @method _loadImages
* @syntax Loader._loadImages()
* @returns {Loader}
* @private
*/
Loader.prototype._loadImages = function()
{
/* Loop trough image data list */
for (var i = this._imageDataList.length - 1; i >= 0; i--)
{
/* Load image */
this._loadImage(this._imageDataList[i]);
}
/* Return this */
return this;
};
/**
* @method _loadSounds
* @syntax Loader._loadSounds()
* @returns {Loader}
* @private
*/
Loader.prototype._loadSounds = function()
{
/* Loop trough sound data list */
for (var i = this._soundDataList.length - 1; i >= 0; i--)
{
/* Load sound */
this._loadSound(this._soundDataList[i]);
}
/* Return this */
return this;
};
/**
* @method addImage
* @syntax Loader.addImage(path, name, extension)
* @param {String} path
* @param {String} name
* @param {String} extension
* @returns {Loaded}
* @public
*/
Loader.prototype.addImage = function(path, name, extension)
{
/* Add image data to image data list */
this._imageDataList.push(
{
path: path,
name: name,
extension: extension
});
/* Return this */
return this;
};
/**
* @method addSound
* @syntax Loader.addSound(path, name, extension)
* @param {String} path
* @param {String} name
* @param {Array} extensions
* @returns {Loaded}
* @public
*/
Loader.prototype.addSound = function(path, name, extensions)
{
/* Add sound data to sound data list */
this._soundDataList.push(
{
path: path,
name: name,
extensions: extensions
});
/* Return this */
return this;
};
/**
* @method progress
* @syntax Loader.progress()
* @returns {Uint}
* @public
*/
Loader.prototype.progress = function()
{
/* Calculate total */
var total = (this._imageDataList.length + this._soundDataList.length);
/* Calculate total loaded */
var totalLoaded = this._imagesLoaded + this._soundsLoaded;
/* Return progress */
return ((100 / total) * totalLoaded).toFixed(2);
};
/**
* @method run
* @syntax Loader.run()
* @returns {Loader}
* @public
*/
Loader.prototype.run = function()
{
/* Load images */
this._loadImages();
/* Load sounds */
this._loadSounds();
/* Return this */
return this;
};
Gewijzigd op 26/07/2014 23:26:35 door Lord Gaga
Geen idee hoe je het oplost, maar ik kan me wel voorstellen dat een browser crasht als er 648 geluidsbestanden moeten worden ingeladen. Kun je ze niet in "gedeeltes" inladen? Bijv. afhankelijk van het leven waar je in zit? En heb je echt 648 geluiden nodig op 1 pagina?
En ik heb nog eens goed gekeken en ik denk inderdaad dat de browser crasht omdat er in 1 keer 648 requests worden gedaan. Zou het helpen als ik een queue maak en de bestanden 1 voor 1 laadt? Dan wordt het volgende bestand pas geladen als het vorige bestand het event 'load' fired.
Ik heb hier iets gevonden:
https://developer.mozilla.org/nl/docs/Web/Guide/HTML/Using_HTML5_audio_and_video#Seeking_through_media
Edit:
Het werkt in Chrome, maar Firefox stopt al met laden na 2 bestanden, ook als ik maar 10 bestanden wil downloaden. Ik download ze 1 voor 1 (de volgende wordt geladen bij sound.one('canplaythrough', function() { // download nieuwe });
Weet iemand nog een andere manier die wél in alle browsers werkt?
Gewijzigd op 27/07/2014 16:35:33 door Lord Gaga
Waarom laad je ze niet pas in wanneer mensen ze selecteren? Kan me niet voorstellen dat er een enorme vertraging plaatsvindt wanneer de duur van de fragmenten 1 tot 6 seconden is.
Dat zou kunnen, maar ik heb ook nog afbeeldingen die ik wél van te voren moet laden. Dit zijn er ook nogal wat en Firefox crasht of wordt heel traag wanneer ik afbeeldingen preload. Doe ik misschien iets verkeerd? Heeft iemand een tutorial / voorbeeld / plugin die ik kan gebruiken?
Afbeeldingen gewoon inladen met CSS geprobeerd? Moeten we heel veel zijn wil dat voor grote vertraging zorgen.
Ik zou zeggen probeer mijn idee gewoon eens. Je hoeft enkel even ergens een wat groter geluidsbestand te downloaden en dit dan opnemen in je webpagina en dan een fragment ervan laten afspelen.