I did some research and apparently the function that gets called for preloading elements from the queue is fired twice (once from the cache). (I've already checked and each element is added to the queue only once.)
However, this results in two video elements being created, as I'm added them to my document in my function that handles files from the queue. (Basically, I'm creating a bunch of videos with height 0px, which I'm planning to show one by one later on. However, I end up with duplicates of each video.)
var onPreloadFile = function(event) {
var item = event.item; // A reference to the item that was passed in to the LoadQueue
var type = item.type;
if (type == 'video') {
var video = document.createElement('video');
video.src = item.src;
video.setAttribute('class', item.css_class + ' vidStim');
video.setAttribute('style', 'height: 0px');
document.body.children.videoContainer.appendChild(video);
}
}
How can I go about fixing this problem so that I don't have duplicates? (Note -- there is the possibility I want the same video to show up again later on, so I can't only append children that I haven't appended before.)
UPDATE: This fixes my problem, but I don't know if it's bad practice.
var alreadyInstalled = []; //array for vids I put in the document
var onPreloadFile = function(event) {
var item = event.item; // A reference to the item that was passed in to the LoadQueue
var type = item.type;
if (type == 'video') {
var video = document.createElement('video');
video.src = item.src;
video.setAttribute('class', item.css_class + ' vidStim');
video.setAttribute('style', 'height: 0px');
if (alreadyInstalled.length > 0){
//checking previous vid to see if it was the same one. this works because i'm assuming the two duplicate firing happen one after the other. is this always the case?
if (alreadyInstalled[alreadyInstalled.length-1] != video.src) {
alreadyInstalled.push(video.src);
document.body.children.videoContainer.appendChild(video);
}
else {
alreadyInstalled.push(video.src);
document.body.children.videoContainer.appendChild(video);
}
document.body.children.videoContainer.appendChild(video);
}
}