gpt4 book ai didi

javascript - jQuery:get() 比 load() 慢

转载 作者:数据小太阳 更新时间:2023-10-29 04:51:18 25 4
gpt4 key购买 nike

显而易见的问题 - 为什么?

我需要从外部页表单元格中获取,然后将其注入(inject)当前页。使用了复杂的选择器。

这是 .load():

$('#check').load('https://bla-bla-bla .small:contains(Something)+.small:lt(1)');

这是 .get():

function showGetResult()
{
var result = null;
var scriptUrl = "https://bla-bla-bla";
$.get(scriptUrl, function(data) {
result = $(".small:contains(Something)", data).next().html() || "Error";
$('#check').append(result);
});
}

load() 以平均 1-2 秒的速度更快地获取数据。但我喜欢 get() - 因为我可以获得字符串结果,而不是对象。

有人可以解释为什么 load() 运行得更快吗?

最佳答案

因为我很想自己找到这个问题的答案,所以我认为提供一些资源来帮助会很好:


AJAX jQuery.load versus jQuery.get :

First of all those two functions are completely different. The 'load' function works with selectors and loads the result of AJAX call inside the selected group and the callback is to handle the "oncomplete" event of the call; while the $.get function is more general and the callback handles the success response of AJAX call where you are free to define any behavior you want. And you can find all this information just by looking at the documentation and specification of the jQuery framework.


Difference between $.ajax() and $.get() and $.load()

$.get() is just a shorthand for $.ajax() but abstracts some of the configurations away, setting reasonable default values for what it hides from you. Returns the data to a callback. It only allows GET-requests so is accompanied by the $.post() function for similar abstraction, only for POST

.load() is similar to $.get() but adds functionality which allows you to define where in the document the returned data is to be inserted. Therefore really only usable when the call only will result in HTML. It is called slightly differently than the other, global, calls, as it is a method tied to a particular jQuery-wrapped DOM element. Therefore, one would do: $('#divWantingContent').load(...)


似乎 $.get$.load 都使用了 $.ajax 功能,但方式不同。也许性能差异在于解析返回数据所需的时间?

也许真正的问题是将请求发送到外部 URL 需要多长时间(“ping”时间),然后这与处理返回数据相比如何?

关于javascript - jQuery:get() 比 load() 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20771879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com