Bug Tracker

Ticket #6185: 0001-ready-fn-should-not-run-synchronously.patch

File 0001-ready-fn-should-not-run-synchronously.patch, 1.4 KB (added by hallettj, 13 years ago)

patch that wraps invocation of a callback with setTimeout()

  • src/core.js

    From 002065ff5cfd58b82a00af614eff3d02222c74bb Mon Sep 17 00:00:00 2001
    From: Jesse Hallett <[email protected]>
    Date: Fri, 26 Feb 2010 13:26:16 -0800
    Subject: [PATCH] ready( fn ) should not run synchronously.
    
    ---
     src/core.js       |    6 ++++--
     test/unit/core.js |   15 +++++++++++++++
     2 files changed, 19 insertions(+), 2 deletions(-)
    
    diff --git a/src/core.js b/src/core.js
    index ba5d7f5..6baadb0 100644
    a b jQuery.fn = jQuery.prototype = { 
    234234
    235235                // If the DOM is already ready
    236236                if ( jQuery.isReady ) {
    237                         // Execute the function immediately
    238                         fn.call( document, jQuery );
     237                        // Execute the function on the next tick
     238                        setTimeout(function() {
     239                                fn.call( document, jQuery );
     240                        }, 1);
    239241
    240242                // Otherwise, remember the function for later
    241243                } else if ( readyList ) {
  • test/unit/core.js

    diff --git a/test/unit/core.js b/test/unit/core.js
    index d8aba16..c33361b 100644
    a b test("jQuery.parseJSON", function(){ 
    832832                ok( true, "Test malformed JSON string." );
    833833        }
    834834});
     835
     836asyncTest("ready(), after DOM is ready", function(){
     837        expect(3);
     838
     839        equals( jQuery.isReady, true, "DOM is ready" );
     840
     841        var callbackCalled = false;
     842        jQuery(document).ready(function(){
     843                callbackCalled = true;
     844                ok( true, "ready() callback is called" );
     845                start();
     846        });
     847
     848        equals( callbackCalled, false, "ready() callback is called asynchronously" );
     849});