简体   繁体   English

jQuery:为什么要在执行beforeSend中的代码之前执行ajax请求?

[英]JQuery: Why does ajax request execute before code in beforeSend finishes executing?

This is my code. 这是我的代码。 Basically I have a Reload.gif that I want to start playing on my page before I send an ajax request to reload my data. 基本上,在发送ajax请求以重新加载数据之前,我想在页面上开始播放Reload.gif。 However what ends up happening is it will only start playing after my success function is called. 但是最终发生的事情是它只会在调用我的成功函数后才开始播放。 This is what the timeline looks like: 时间线如下所示:

0:00 seconds: 1 logged to console 0:00秒:1登录到控制台

0:00 seconds: 2 logged to console 0:00秒:2已登录控制台

0:10 seconds: 3 logged to console 0:10秒:3登录到控制台

0:10 seconds: GIF starts playing. 0:10秒:GIF开始播放。

This doesn't make sense, is setting the src of an img async or something? 这没有道理,是将img的src设置为async还是其他? Image code: 图片代码:

<img id="reloadIcon" src="/img/Reload.png" style="width: 25px; height: 25px"/>

jQuery.ajax({
    url: url,
    type: 'GET',
    timeout: 20000,
    beforeSend: function() {
        console.log(1);
        document.getElementById('reloadIcon').src = "/img/Reload.gif";
        console.log(2);
    },
    success: function (result) {
        console.log(3);
    }
});

Load the image before $.ajax() call. $.ajax()调用之前加载图像。 Toggle the CSS display property of the <img> element at beforeSend function, instead of changing the .src of the <img> element. 切换CSS display的属性的<img>在元件beforeSend而不是改变的功能, .src所述的<img>的元素。

jQuery.ajax({
    url: url,
    type: 'GET',
    timeout: 20000,
    beforeSend: function() {
        console.log(1);
        document.getElementById('reloadIcon').style.display = "block";
        console.log(2);
    },
    success: function (result) {
        document.getElementById('reloadIcon').style.display = "none";
        console.log(3);
    }
    , error: function() {
        document.getElementById('reloadIcon').style.display = "none";
    }
});

Fetching the image by changing "src" is asynchronous, I think you can just set the src when the page is load but make the image element invisible. 通过更改“ src”来获取图像是异步的,我认为您可以在页面加载时设置src,但是使图像元素不可见。 And set the image visible when ajax begin. 并在ajax开始时将图像设置为可见。

If you change the src attribute, the browser must complete a bunch of things like look for the image in its cache, probably re-request the image, parse, aso 如果更改src属性,则浏览器必须完成一堆事情,例如在其缓存中查找图像,可能会重新请求图像,解析,aso

Second, your JS code shares one thread with a lot of other things. 其次,您的JS代码与许多其他事情共享一个线程。 Browsers tend to internally cache CSS assignments and execute the following JS code first, as long as the right-then missing, cached changes will not affect the functionality of your code. 浏览器倾向于在内部缓存CSS分配并首先执行以下JS代码,只要先丢失然后缓存的更改就不会影响代码的功能。 If you want to force assignment in this case, read the specific CSS property, and the browser will assign the changes before. 如果在这种情况下要强制分配,请阅读特定的CSS属性,浏览器将在之前分配更改。 I tend to state (without having tested), there's such behaviour on attributes as well. 我倾向于指出(未经测试),属性上也有这种行为。

Best practice in this case is the post from guest271314, hiding and showing the outer container is much faster and reliable. 在这种情况下,最佳实践是guest271314的帖子,隐藏并显示外部容器要更快,更可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM