基本上當你需要做的是替換具有
jQuery.ready();
與要調用的函數的名稱線。如果你想要的東西像jQuery的ready註冊方法一樣工作,建立一個產生隊列的函數。當「就緒」被觸發時,循環通過隊列。
您需要了解更多信息,以下是一個不使用超時的快速而髒的示例。這不是生產準備,只是一個基本的POC。
(function() {
var ready = {
_readyQueue: [],
_hasRun: false,
_docReadyCalled : function() {
this._hasRun = true;
this._execute();
},
_execute: function() {
var func;
while (this._readyQueue.length) {
func = this._readyQueue.shift();
func();
}
},
register: function (func) {
this._readyQueue.push(func);
if (this._hasRun) {
this._execute();
}
}
}
window.docReady = ready.register.bind(ready); //use what ever global namespace you want here
function bindReady() {
/* This would be that jQuery code, I am just use window load here so not so much code */
//Not all browser support this, quick and dirty for example
window.addEventListener('load', ready._docReadyCalled.bind(ready), false);
}
bindReady();
})();
/* waiting for DOM to be ready */
docReady(function() { console.log("here"); });
docReady(function() { console.log("there"); });
/* Showing what happens when you call docReady after it is ready */
docReady(function() { console.log("registering ready again"); docReady(function() { console.log("I am here!"); }); });
你可以試試'window.onload = function(){}',這可能對你來說已經足夠了。 –
@RocketHazmat根據我的理解,只有在所有元素(包括圖像)加載完成後,纔會觸發'window.onload'。 '$(document).ready()'僅在HTML加載完成時觸發。由於我的頁面上有很多大圖片,因此我不希望我的用戶在運行腳本之前等待加載圖像。 – Jon