mardi 21 avril 2015

can't keep a index using setTimeout

i'm developping a new javascript feature, in which i have a collection of media (img,video,txt...), the user can add a new media and once the media is added to the collection a new asychronous request is send to the server for checking if the added media thumbail already exist. if so the thumbnail is added to the media element in the collection. this the code that check if the thumbnail exist:




     if (!img.width) {
                                setTimeout(_.bind(function () {
                                    if (img.width) {
                                        this.$("mySelector").first().removeClass("nocover ").css('background-image', 'url('url of thuambnail');
                                    }
                                }, this), 15000);


my probleme is how to keep an index for a element whose check request was send to the server,because since this is ansychronouse function smoetimes while the request of the thumbnail check is being processed the user can add a new media element an then the thumbnail is added to wrong element. because my only way to refer to the new element is : this.$("mySelector").first()

Aucun commentaire:

Enregistrer un commentaire