ajax aftersuccess,確保在$(document).ready()之后發生ajax成功函數

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()是否已經運行,如果有,則立即運行回調。或者,它會像往常一樣推遲它。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值