2015-12-15 33 views
0

我有這個js功能運行js函數結束完整

$('#galleria').galleria({ 
    show:<?php echo $show;?>, 
    autoplay: 3000, 
    extend: function(options) { 
    var gallery = this; // "this" is the gallery instance 

    this.bind('image', function(e) { 
     var curr = gallery.getData(gallery.getIndex()); 
     var currOrig = curr.original; 
     var altTxt = $(currOrig).attr('alt'); 
     $(e.imageTarget).attr('alt',altTxt); 
    }); 
    this.bind('thumbnail', function(e) { 
     var thumb = gallery.getData(e.index); 
     var thumbOrig = thumb.original; 
     var altText = $(thumbOrig).attr('alt'); 
     $(e.thumbTarget).attr('alt',altText); 
    }); 
    } 
}) 

Basicaly這是免費的圖像galery。上面的代碼必須包含在根據所有者指令的圖像的html後面:http://galleria.io/

問題是此函數從html重新創建新元素,並且每次頁面加載時都會在不同的時間執行此操作。 js腳本所需的時間會有所不同,因爲使用此庫的每個頁面js都有不同數量的圖像,因此執行時間不同。 我需要運行的另一個功能:

load_img_comments('<?php echo $image;?>','<?php echo $imagename;?>','','i'); 

從準備HTML需要一個元素,並做一些事情吧。如果我將此函數設置爲在文檔就緒或onload上運行,則函數無法找到該元素,因爲第一個函數仍在處理html。我試圖延遲執行secong js函數,但正如我上面所解釋的,每個頁面都有不同數量的圖像,我無法預測第一個函數需要結束多少次。

我該如何圈定這種愚蠢的情況?

請指教。 P.S.

我也試過這樣:

$(document).bind($('#galleria').galleria({ 
    show:<?php echo $show;?>, 
    autoplay: 3000, 
    extend: function(options) { 
    var gallery = this; // "this" is the gallery instance 

    this.bind('image', function(e) { 
     var curr = gallery.getData(gallery.getIndex()); 
     var currOrig = curr.original; 
     var altTxt = $(currOrig).attr('alt'); 
     $(e.imageTarget).attr('alt',altTxt); 
    }); 
    this.bind('thumbnail', function(e) { 
     var thumb = gallery.getData(e.index); 
     var thumbOrig = thumb.original; 
     var altText = $(thumbOrig).attr('alt'); 
     $(e.thumbTarget).attr('alt',altText); 
    }); 
    } 
}), load_img_comments('<?php echo $image;?>','<?php echo $imagename;?>','','i')); 

但它返回的錯誤:類型錯誤:r.push沒有在主要的jquery.js一個函數文件

預先感謝您!

回答

0

這是你如何運行JS功能之前的js函數結束後:

var tempFunc = FunctionToAppendTo; //note: no() at the end 
FunctionToAppendTo = new Function(param1, param2){ 
    tempFunc.apply(this, arguments); 

    //your new code goes here... 

}; 

注意:您可能需要使用正確的「這」呼叫應用。

來源:applygetting arguments


編輯:看來你要附加到廣場的圖像事件。這應該做的伎倆:

$('#galleria').galleria({ 
show:<?php echo $show;?>, 
autoplay: 3000, 
extend: function(options) { 
    var gallery = this; // "this" is the gallery instance 

    this.bind('image', function(e) { 
    var curr = gallery.getData(gallery.getIndex()); 
    var currOrig = curr.original; 
    var altTxt = $(currOrig).attr('alt'); 
    $(e.imageTarget).attr('alt',altTxt); 

    load_img_comments('<?php echo $image;?>','<?php echo $imagename;?>','','i')); 
    }); 
    this.bind('thumbnail', function(e) { 
    var thumb = gallery.getData(e.index); 
    var thumbOrig = thumb.original; 
    var altText = $(thumbOrig).attr('alt'); 
    $(e.thumbTarget).attr('alt',altText); 
    }); 
} 

})

或者你也可以從外面附上您的功能,如解釋here

+0

wotanii,謝謝,但說實話,從你的例子並不清楚我如何改變它..你可以請提供上述兩個函數的例子嗎?謝謝 – thecore7