簡體   English   中英

在先前的js函數結束后運行js函數

[英]Run js function after previous js function ends complete

我有這個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);
    });
  }
})

基本上,這是免費的圖片庫。 根據所有者說明,必須在帶有圖片的html后面緊跟上述代碼: http : //galleria.io/

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

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

它從現成的html中獲取一個元素並對其進行處理。 如果我將此功能設置為在文檔就緒或onload上運行,則該功能找不到元素,因為第一個功能仍在html上運行。 我試圖延遲secong js函數的執行,但是如上所述,每個頁面具有不同數量的圖像,並且我無法預測第一個函數需要結束多少時間。

我該如何解決這種愚蠢的情況?

請指教。 聚苯乙烯

我也試過這個:

$(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'));

但返回錯誤:TypeError:r.push不是主jquery.js文件中的函數

先感謝您 !

這是您在先前的js函數結束后運行js函數的方式:

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

  //your new code goes here...

};

注意:您可能需要使用正確的“ this”才能應用調用。

資料來源: apply獲取參數


編輯:似乎您想附加到圖庫的圖像事件。 這應該可以解決問題:

$('#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);
  });
}

})

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM