SORU
24 AĞUSTOS 2012, Cuma


Animasyonlar ile bir İşlevi kadar bekle başka bir İşlevi çalışan tamamlanana kadar

Normal bir sorun yaşıyorum (non-ajaxçok ilgili işlevlerianimasyonlarherbiri için. Şu anda ben sadece fonksiyonları arasında setTimeout var, ama bu bilgisayar / tarayıcılar aynı olduğu için mükemmel değil.

Ek Not: ayrı animasyonlar hem Onlar çarpışır/etc.

Sadece geri arama işlevi, bir başka koyamam

// multiple dom animations / etc
FunctionOne();

// What I -was- doing to wait till running the next function filled
// with animations, etc

setTimeout(function () { 
    FunctionTwo(); // other dom animations (some triggering on previous ones)
}, 1000); 

İçin jQuery js/: zaten var

// Pseudo-code
-do FunctionOne()
-when finished :: run -> FunctionTwo()

$.when() & $.done(), biliyorum ama bu AJAX için


  • GÜNCELLEME BENİM ÇÖZÜM

jQuery maruz bir değişken var nedense her yerde jQuery dokümanlar listede olmayan bir () adlı $.animasyonlar, şu anda gerçekleşen bir dizi tutan zamanlayıcılar,.

function animationsTest (callback) {
    // Test if ANY/ALL page animations are currently active

    var testAnimationInterval = setInterval(function () {
        if (! $.timers.length) { // any page animations finished
            clearInterval(testAnimationInterval);
            callback();
        }
    }, 25);
};

Temel kullanım:

functionOne(); // run some function with animations etc

animationsTest(function () {
    // your callback (things to do after all animations are done)
});

CEVAP
27 AĞUSTOS 2012, PAZARTESİ


$.Deferred jQuery kullanabilirsiniz

var FunctionOne = function () {
  // create a deferred object
  var r = $.Deferred();

  // do whatever you want (e.g. ajax/animations other asyc tasks)

  setTimeout(function () {
    // and call `resolve` on the deferred object, once you're done
    r.resolve();
  }, 2500);

  // return the deferred object
  return r;
};

// define FunctionTwo as needed
var FunctionTwo = function () {
  console.log('FunctionTwo');
};

// call FunctionOne and use the `done` method
// with `FunctionTwo` as it's parameter
FunctionOne().done(FunctionTwo);

ayrıca birden fazla deferreds birlikte paketi olabilir:

var FunctionOne = function () {
  var
    a = $.Deferred(),
    b = $.Deferred();

  // some fake asyc task
  setTimeout(function () {
    console.log('a done');
    a.resolve();
  }, Math.random() * 4000);

  // some other fake asyc task
  setTimeout(function () {
    console.log('b done');
    b.resolve();
  }, Math.random() * 4000);

  return $.Deferred(function (def) {
    $.when(a, b).done(function () {
      def.resolve();
    });
  });
};

http://jsfiddle.net/p22dK/

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • David Wills

    David Wills

    31 Aralık 2007
  • fouseyTUBE

    fouseyTUBE

    21 Mart 2011
  • Jack Vale Films

    Jack Vale Fi

    8 ŞUBAT 2007