开发者

Jquery Plugin / Set interval and scoping issue

I am trying to write a simple JQuery plugin to do the following-- I need to pass in a single element, like $("div").applyPlugin(); and then have it go through each of the selectors i passed in and set an interval that is local to THAT instance of the div, and then clear it some time later. I am trying to write a plugin to help accomplish this, but there seems to be a scoping issue and im not really sure whats going on.

Again, I need to setup a timer on a div, and be able to reuse it like 10 times on a page. All divs will have their own 'timers' essentially, that when you re-mouse over the div, it will clear that timer on itself. Mouse off, restarts the timer, etc.

I am uncertain whether this has to be accomplished using internal methods to the plugin, or if theres a better/easier way to do this.

Here is where I am at so far with this- '

(function( $ ){

var methods = {
    init : function( options ) { 
        /* init method */
        return setInterval( $(this).b开发者_如何学Pythonoxit('update'),5000);  
    },
    show : function( ) {    },
    hide : function( ) {  },
    update : function( content ) { 
        /* update method */
        console.log("ping");
    }
};

$.fn.boxit = function( method ) {
    // Method calling logic
    if ( methods[method] ) {
        return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
        return methods.init.apply( this, arguments );
    } else {
        $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }           
};

})( jQuery );

'


I could be going the complete wrong direction here so forgive me if this isn't what you want, but you could pass the reference to the jQuery object as a parameter.

(function( $ ){

var methods = {
    init : function( jqObject, options ) { 
        /* init method */
        return setInterval( jqObject.boxit('update'),5000);  
    },
    show : function( ) {    },
    hide : function( ) {  },
    update : function( content ) { 
        /* update method */
        console.log("ping");
    }
};

$.fn.boxit = function( method ) {
    var args = Array.prototype.slice.call( arguments, 0 );
    args.splice(0, 0, $(this));

    // Method calling logic
    if ( methods[method] ) {
        return methods[ method ].apply( this, args.slice( 1 ));
    } else if ( typeof method === 'object' || ! method ) {
        return methods.init.apply( this, args );
    } else {
        $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }           
};

})( jQuery );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜