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()是否已经运行,如果有,则立即运行回调。或者,它会像往常一样推迟它。
#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()是否已经运行,如果有,则立即运行回调。或者,它会像往常一样推迟它。