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

前端 未结 3 1382
不知归路
不知归路 2021-02-20 04:02

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:37

    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'):
    });
    

提交回复
热议问题