简体   繁体   English

Javascript - 取消 onload 事件

[英]Javascript - Cancel onload event

    var town_imgs = $('.town_img img');
    var container;
    var imggg
    town_imgs.hover(function(){

        container = $('<div class="town_img_thmb">'
                +'<span class="thmb_container ajax2"></span>'
            +'</div>').appendTo($(this).parents('.town_wrapper').find('.thmb_wrapper'));

        var imggg = new Image(140,100);             
        imggg.onload = function(){
            container.find('.thmb_container').append(this);
        }
        imggg.src = $(this).attr('src').replace(/\/t0\//,'/t1/');               

    },function(){

        container.remove();
        $(imggg).unbind('onload');

    });

Isn't working when I'm hovering thumbnails very fast.当我快速悬停缩略图时不起作用。 It displays 2-3 images in a row for a about 250-500ms~它连续显示2-3张图像约250-500ms~

As I understand it happens because I'm using outside variable to store current thumbnail.据我了解,这是因为我使用外部变量来存储当前缩略图。

Is it?是吗?

Is there solution to cancel onload event properly?是否有正确取消 onload 事件的解决方案?

Thanks ;)谢谢;)

There are 2 problems, you want to remove var from this, so your reference is correct:有两个问题,你想从中删除var ,所以你的参考是正确的:

var imggg = new Image(140,100);

Then unbind by clearing the onload function you set:然后通过清除您设置的onload函数来解除绑定:

imggg.onload = null;

You can see a working demo here .您可以在此处查看工作演示

If you want to Cancel/destroy onload event using jQuery如果您想使用jQuery取消/销毁onload事件

You can use unbind function.您可以使用unbind功能。

$('#myimage').unbind('click');

You can get more help from here:您可以从这里获得更多帮助:
Best way to remove an event handler in jQuery? 在 jQuery 中删除事件处理程序的最佳方法?

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

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