Can I get a jQuery Deferred on document.ready()?

后端 未结 6 1401
失恋的感觉
失恋的感觉 2020-12-14 07:47

Right after my script is loaded I am making an Ajax request to get some translations. This should always return after the document is ready since I am loading my scripts at

6条回答
  •  轮回少年
    2020-12-14 08:28

    Here's a cleaned up version of ircmaxell's comment:

    (function() {
      var doc_ready = $.Deferred();
      $(doc_ready.resolve);
      $.when(doc_ready, $.ajax('translations')).then(function() {
        console.log("done");
      });
    })();
    

    edit

    Some clarification to stop the incorrect edits:

    Passing a function to the jquery object (e.g. $(some_func)) is the same as $(document).ready(some_func).

    Therefore, the $(doc_ready.resolve); line is just shorthand for something like this:

    $(document).ready(function() {
      doc_ready.resolve()
    });
    

提交回复
热议问题