简体   繁体   English

jQuery 从整个阵列取消绑定事件

[英]jQuery unbind event from whole array

Hi does anybody know how is jquery unbind function woking?I have a jquery powered photogallery where I am binding some stuff on load event(change image and so on)..But when the user is quickly walking through the gallery I want to unbind "older" events and than bind load event only on the last image. Hi does anybody know how is jquery unbind function woking?I have a jquery powered photogallery where I am binding some stuff on load event(change image and so on)..But when the user is quickly walking through the gallery I want to unbind "旧”事件,而不是仅在最后一个图像上绑定加载事件。 Otherwise the user will see every image he walked through for a tiny moment and I only want to show the last one..(performance, UX,..)I store every loaded(not exactly loaded, too every requested) image in an array, so everytime I call my loadImage function I call jQuery(myArrayWithImages).unbind("load.backgroundImagesLoader") but it seems it does not work, the load events are still there (its a bit weird, I cant find it in debugger, but I see it:)) Is there any way to unbind the events from whole array, or from whole js instance, except do it through foreach?否则用户会看到他走过的每张图片一小会儿,我只想显示最后一张..(性能,UX,..)我将每个加载的(不是完全加载的,也是每个请求的)图像存储在一个数组中,所以每次我调用我的 loadImage function 我调用jQuery(myArrayWithImages).unbind("load.backgroundImagesLoader")但它似乎不起作用,加载事件仍然存在(它有点奇怪,我在调试器中找不到它,但我看到了:)) 有没有办法从整个数组或整个 js 实例中解除绑定事件,除了通过 foreach 执行吗?

Here is some code snippet..It is whole loadImage function..这是一些代码片段..它是整个loadImage function ..

function loadImage(index , showLoading, callback){
    if(backgroundImages==undefined) backgroundImages=Array();
    //if(backgroundImages[window.location.hash][index]==undefined)backgroundImages[window.location.hash][index] = Array();

            jQuery("#galleryEl .sig_thumb a").each( function( pos , el ) {
            var wh_bg_b = getVierwportSize();
            if(index == pos){
                    var bg_img_path_b = jQuery(el).attr('href');
                    var size = 'max';
                    if ( bg_img_path_b != undefined ) {
                    if ( (wh_bg_b[0] < 1281) && (wh_bg_b[1] < 801) ) {
                        size = 'min';
                        bg_img_path_b = bg_img_path_b.substring( 0, bg_img_path_b.lastIndexOf("/") ) + "/1280/" + bg_img_path_b.substring( bg_img_path_b.lastIndexOf("/")+1 );
                    } else if ( (wh_bg_b[0] < 1441) && (wh_bg_b[1] < 901) ) {
                        size = 'med';
                        bg_img_path_b = bg_img_path_b.substring( 0, bg_img_path_b.lastIndexOf("/") ) + "/1440/" + bg_img_path_b.substring( bg_img_path_b.lastIndexOf("/")+1 );
                    }
                }


                    console.log("test");
                    if(backgroundImages[bg_img_path_b]!=undefined){
                        if(backgroundImages[bg_img_path_b].loaded=="true"){
                        //console.log(index+" "+backgroundImages[window.location.hash][index][size]['loaded']);
                        if(typeof callback=='function'){
                            callback.call(this, bg_img_path_b);
                        }   }
                        else if(backgroundImages[bg_img_path_b].loaded=="loading"){
                            jQuery(backgroundImages).unbind('load.backgroundImages');

                            jQuery(backgroundImages[bg_img_path_b]).bind('load.backgroundImages',function(){
                                backgroundImages[bg_img_path_b].loaded="true";
                                if(typeof callback=='function'){
                                    callback.call(this, bg_img_path_b);
                                    //console.log("loaded "+index);
                                }
                            });
                        }
                    }
                    else{


                    backgroundImages[bg_img_path_b]=new Image();
                    backgroundImages[bg_img_path_b].src = bg_img_path_b;
                    backgroundImages[bg_img_path_b].loaded="loading";
                    jQuery(backgroundImages).unbind('load.backgroundImages');

                    jQuery(backgroundImages[bg_img_path_b]).bind('load.backgroundImages',function(){
                        backgroundImages[bg_img_path_b].loaded="true";
                        if(typeof callback=='function'){
                            callback.call(this, bg_img_path_b);
                            //console.log("loaded "+index);
                        }
                    });
                }
                console.log(backgroundImages[bg_img_path_b]);

                //console.log(size);
                //console.log(backgroundImages);


            }


    } );

} }

You need to unbind not like你需要解绑不喜欢

jQuery(myArrayWithImages).unbind("load.backgroundImagesLoader")

but

jQuery(myArrayWithImages).unbind(load.backgroundImagesLoader).

Just read manual, you can use namespaces:只需阅读手册,您就可以使用命名空间:

$('#foo').bind('click.myEvents', handler);

and

$('#foo').unbind('click.myEvents');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM