Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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~

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?

Thanks ;)

like image 450
Somebody Avatar asked Jan 21 '23 10:01

Somebody


2 Answers

There are 2 problems, you want to remove var from this, so your reference is correct:

var imggg = new Image(140,100);

Then unbind by clearing the onload function you set:

imggg.onload = null;

You can see a working demo here.

like image 188
Nick Craver Avatar answered Feb 01 '23 13:02

Nick Craver


If you want to Cancel/destroy onload event using jQuery

You can use unbind function.

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

You can get more help from here:
Best way to remove an event handler in jQuery?

like image 33
Rohit Tagadiya Avatar answered Feb 01 '23 13:02

Rohit Tagadiya