Running a function just before $(document).ready() triggers

前端 未结 3 1348
别那么骄傲
别那么骄傲 2021-02-20 03:40

I\'ve attached multiple functions in multiple files to $(document).ready and would like to attach a single function to happen before them as either the first that $(document).re

3条回答
  •  孤城傲影
    2021-02-20 04:34

    This would be a time where I would trigger a custom event that all of your other files would bind to, you would only have one ready handler, which would do stuff, then trigger the custom event.

    So, somewhere else in your code, instead of using $(document).ready, you would use

    $(window).bind('setup', function() {
       //code here...
    });
    

    And/or

    $(window).bind('loaded', function() {
       //some other code here...
    });
    

    And in your one and only ready handler, you'd do something like this:

    $(document).ready(function() {
       $(window).trigger('setup');
       $(window).trigger('loaded'):
    });
    

提交回复
热议问题