SORU
2 NİSAN 2011, CUMARTESİ


Nasıl bir eleman var kadar beklemek?

Chrome Uzantısı üzerinde çalışıyorum, ve ben merak ediyorum: bir element meydana geldiğinde öğrenmek için en iyi yolu nedir? Düz javascript, bir eleman kadar Çek var ara ile kullanarak, ya da bir WordPress kullanmak bunu yapmanın kolay bir yolu var mı?

CEVAP
4 Aralık 2012, Salı


Bu aynı problemim vardı, devam ettim ve plugin yazdığı için.

$(selector).waitUntilExists(function);

Kod:

(function ($) {

/**
* @function
* @property {object} jQuery plugin which runs handler function once specified element is inserted into the DOM
* @param {function} handler A function to execute at the time when the element is inserted
* @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation
* @example $(selector).waitUntilExists(function);
*/

$.fn.waitUntilExists    = function (handler, shouldRunHandlerOnce, isChild) {
    var found       = 'found';
    var $this       = $(this.selector);
    var $elements   = $this.not(function () { return $(this).data(found); }).each(handler).data(found, true);

    if (!isChild)
    {
        (window.waitUntilExists_Intervals = window.waitUntilExists_Intervals || {})[this.selector] =
            window.setInterval(function () { $this.waitUntilExists(handler, shouldRunHandlerOnce, true); }, 500)
        ;
    }
    else if (shouldRunHandlerOnce && $elements.length)
    {
        window.clearInterval(window.waitUntilExists_Intervals[this.selector]);
    }

    return $this;
}

}(jQuery));

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • AndyMcMillinTV

    AndyMcMillin

    6 HAZİRAN 2007
  • Max Lee

    Max Lee

    18 AĞUSTOS 2006
  • TWiT Netcast Network

    TWiT Netcast

    27 EKİM 2005