I'm trying to speed up some of my sites by moving around the ajax calls
我試圖通過移動ajax調用來加速我的一些網站
The situation is as follows:
情況如下:
I have a page with some canvas in it + other data.
我有一個頁面,里面有一些畫布+其他數據。
When the page is requested the 'other data' is loaded. In $(document).ready(); I have a function which gets the data for the canvas. This is done using ajax.
請求頁面時,會加載“其他數據”。在$(document).ready();我有一個獲取畫布數據的函數。這是使用ajax完成的。
I'm wondering if I could make the ajax call before the $(document).ready() but the success of the ajax call( ie rendering the canvas) only when the dom is ready.
我想知道我是否可以在$(document).ready()之前進行ajax調用,但是只有當dom准備好時才能成功調用ajax(即渲染畫布)。
So:
$(document).ready(function(){
$.ajax({
url: '/someurl'
dataType: 'json',
success: function(response){
startDrawOnCanvas(stage, response.data);
}
});
}
This works, but I'd like something like this
這有效,但我喜歡這樣的東西
$(document).ready(function(){
}
$.ajax({
url: '/someurl'
dataType: 'json',
success: function(response){
//only fire once dom is ready for it
startDrawOnCanvas(stage, response.data);
}
});
1 个解决方案
#1
5
You can do;
你可以做;
$.ajax({
url: '/someurl'
dataType: 'json',
success: function(response){
$(document).ready(function () {
//will fire once dom is ready for it
startDrawOnCanvas(stage, response.data);
});
}
});
... jQuery will detect whether $(document).ready() has already ran, and run the callback immediately if it has. Alternately, it'll defer it like it always does.
... jQuery將檢測$(document).ready()是否已經運行,如果有,則立即運行回調。或者,它會像往常一樣推遲它。