Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Quick Description: I'm aware that using $(this) in a function won't work because it's not within the right scope. I've also seen other similar questions. I just still can't figure out how to fix my scenerio.

Goal: I'm trying to build a panoramic photo viewer with jQuery. I have it working, but I need multiple instances. So I need to target only the one I'm hovering on.

Code:

jsFiddle: http://jsfiddle.net/kthornbloom/5J3rh/

Simplified Code:

var hoverInterval;

function doStuff() {

/* The next line is the one in question */

    $(this).animate({
      /* stuff happening */
    });
}

$(function() {
    $('.pan-wrap').hover(
        function() {
            /* stuff happening */
            hoverInterval = setInterval(doStuff, 250);
        },
        function() {
            clearInterval(hoverInterval);
   });
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
303 views
Welcome To Ask or Share your Answers For Others

1 Answer

You have scope issues, this in the doStuff is window context.

Use proxy()

hoverInterval = setInterval($.proxy(doStuff,this), 250);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...