开发者

jQuery $.get being called multiple times...why?

I am building this slideshow, hereby a temp URL:

http://ferdy.dnsalias.com/apps/jungledragon/html/tag/96/homepage/slideshow/mostcomments

There are multiple ways to navigate, clicking the big image goes to the next image, clicking the arrows go to the next or previous image, and you can use your keyboard arrows as well. All of these events call a method loadImage (in slideshow.js).

The image loading is fine, however at the end of that routine I'm making a remote Ajax call using $.get. The purpose of this call is to count the view of that image. Here is the pseudo, snipped:

function loadImage(id,url) {

        // general image loading routine

        // enable loader indicator
        $("#loading").show();

        var imagePreloader = new Image();
        ima开发者_JAVA百科gePreloader.src = url;
        loading = true;
        $(imagePreloader).imagesLoaded(function() {

            // load completed, hide the loading indicator
            $("#loading").hide();

            // set the image src, this effectively shows the image
            var img = $("#bigimage img");
            img.attr({ src: url, id: id });
            imageStartTime = new Date().getTime();

            // reset the image dimensions based upon its orientation
            var wide = imagePreloader.width >= imagePreloader.height;
            if (wide) {
                img.addClass('wide');
                img.removeClass('high');
                img.removeAttr('height');
            } else {
                img.addClass('high');
                img.removeClass('wide');
                img.removeAttr('width');
            }

            // update thumb status
            $(".photos li.active").removeClass('active');
            $("#li-" + id).addClass('active');

            // get the title and other attributes from the active thumb and set it on the big image
            var imgTitle = $("#li-" + id + " a").attr('title');
            var userID = $("#li-" + id + " a").attr('data-user_id');
            var userName = $("#li-" + id + " a").attr('data-user_name');

            $(".caption").fadeOut(400,function(){
                $(".caption h1").html('<a href="' + basepath + 'image/' + id + '">' + imgTitle + '</a>');
                $(".caption small").html('Uploaded by <a href="' + basepath + 'user/' + userID + '">' + userName + '</a>');
                $(".caption").fadeIn();
            });

            // update counter
            $(".counter").fadeOut(400,function() { $(".counter").text(parseInt($('.photos li.active .photo').attr('rel'))+1).fadeIn(); });

        // call image view recording function
        $.get(basepath + "image/" + id + "/record/human");

        // loading routine completed
        loading = false;

}

There is a lot of stuff in there that is not relevant. At the end you can see I am doing the $.get call. The problem is that it is triggered in very strange ways. The first time I navigate to a tumb, it is called once. The next time it is triggered twice. After that, it is triggered 2 or 3 times per navigation action, usually 3.

I figured it must be that my events return multiple elements and therefore call the loadimage routine multiple times. So I placed log statements in both the events and the loadimage routine. It turns out loadimage is called correctly, only once per click.

This means that it seems that the $.get is doing this within the context of a single call. I'm stunned.


Your problem may be:.imagesLoaded is a jQuery plug in that runs through all images on the page. If you want to attach a load event to the imagePreloader only, use

$(imagePreloader).load(function() {
   ...
}

Otherwise, please provide the code where you call the loadImage() function.

Update: when clicking on a thumb That is the problem. $(".photos li a").live('click',... should only be called once on page load. Adding a click handler every time a thumb is clicked will not remove the previous handlers.

Another option is to change the code to $(".photos li a").unbind('click').live('click', ... which will remove the previously registered click handlers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜