0

ここの方向に従って名前間隔を使用してjQueryプラグインを構築しようとしています: http://docs.jquery.com/Plugins/Authoring#Namespacing

今、プラグインが setTimeout を使用してそのメソッドの 1 つを起動する必要があるという問題に遭遇しました。

var jScrollerMethods = {
    ready:function(){
        return this.each(function(){
        var self = this,
    $this = $(this),
        data = $this.data('jScroller');

        settings.timeoutHandle = setTimeout(function(){
            if(settings.direction = "left"){
                this.moveLeft();
            }else if(settings.direction = "right"){
                this.moveRight();
            }
        }, settings.time);

        $this.data('jScroller', {
            settings: settings,
            element: this
        });
    });
}
$.fn.jScroller = function(call){
    if ( jScrollerMethods[call] ) {
      return jScrollerMethods[call].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof call === 'object' || ! call ) {
        if (call) { $.extend(settings, call); }
        return jScrollerMethods.init.apply( this, call );
    } else {
        $.error( 'Method ' +  call + ' does not exist on jQuery.jScroller' );
    } 
}

しかし、私が起こるように、setTimeoutはプラグインオブジェクトではなくウィンドウから起動され、プラグインをページごとに複数回使用できるようにしたいので、現在のオブジェクトをウィンドウに保存することはできません。どうすればこれを達成できますか?

4

1 に答える 1

0

this外部で使用return this.eachすると、正確なセレクターの結果が得られることがわかりました

それで、いくつかの作業で私はそれを行うことができました、

var jScrollerMethods = {
    ready:function(){
        selector = this;

        return this.each(function(){
        var self = this,
    $this = $(this),
        data = $this.data('jScroller');

        settings.timeoutHandle = setTimeout(function(){
            if(settings.direction = "left"){
                selector.jScroller("moveLeft");
            }else if(settings.direction = "right"){
                selector.jScroller("moveRight");
            }
        }, settings.time);

        $this.data('jScroller', {
            settings: settings,
            element: this
        });
    });
}
$.fn.jScroller = function(call){
    if ( jScrollerMethods[call] ) {
      return jScrollerMethods[call].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof call === 'object' || ! call ) {
        if (call) { $.extend(settings, call); }
        return jScrollerMethods.init.apply( this, call );
    } else {
        $.error( 'Method ' +  call + ' does not exist on jQuery.jScroller' );
    } 
}
于 2012-07-24T12:28:40.823 に答える