简体   繁体   English

jquery / javascript中重复/多次下载onkeyup事件

[英]repeated/multiple download onkeyup event in jquery/javascript

if my input box contains URL (eg http://kotaku.com/some-article ) via paste() & keyup() my CURL will get the title, description and images of that particular website and then inserts this into my #content 如果我的输入框通过paste()keyup()包含URL(例如http://kotaku.com/some-article ),则我的CURL将获取该特定网站的标题,描述和图像,然后将其插入我的#content

POST- AJAX 邮政AJAX

$("input").on({
'paste': function() {
 var content;
    if (...) {
      $.post("curlfetch.php?url="+ url, {
      }, function(response){
      content = response;
      $('#content').html(response);
},
'keyup': function() {
    if (...) {
       $('#content').html(content);
    }
});

Data being returned 正在返回数据

<div id="image"><img src ... /> </div>
<div id="title"> some title... </div>
<div id="desc"> some text... </div>

what troubles me is during the keyup() event. 我遇到的麻烦是在keyup()事件期间。

whenever i typed something after the link is pasted, it downloads the image repeatedly. 粘贴链接后,每当我键入内容时,它都会重复下载图像。

so, upon paste() event i get this amount of transactions. 因此,在发生paste()事件时,我得到了该数量的交易。 which is correct. 哪个是对的。 在此处输入图片说明

but after i type something, or anything that activates keyup() event, it download the images again. 但是在我键入某些内容或激活keyup()事件的所有内容之后,它将再次下载图像。 if i continue typing after the link, it keeps downloading.. 如果我在链接后继续输入,它将继续下载。

在此处输入图片说明

this is only true to images. 这仅适用于图像。 the title, description doesnt seem to have this issue. 标题,说明似乎没有这个问题。

so, if i remove the image div, and left the title, desc 因此,如果我删除图像div,并留下标题desc

 <!-- <div id="image"><img src ... /> </div> -->
    <div id="title"> some title... </div>
    <div id="desc"> some text... </div>

it works fine. 它工作正常。

i just get repeated downloads of images for every keyup() event. 对于每个keyup()事件,我只会重复下载图像。 how can i prevent on doing this, or how can i properly approach troubleshooting. 我如何防止这样做,或者如何正确进行故障排除。

note: the var content; 注意: var content; is used to store the data being returned by CURL, and just access this on keyup() event. 用于存储CURL返回的数据,只需在keyup()事件上访问它即可。 so i dont have to request for another $post to curl. 因此,我不必再要求$post卷曲。

im sorry if my explanation is a bit vague. 对不起,如果我的解释有点含糊。

By using $('#content').html(content); 通过使用$('#content').html(content); you are constantly re-inserting your image into the DOM. 您一直在将图像重新插入DOM。 Depending on your browser and server settings this may or may not be getting cached. 根据您的浏览器和服务器设置,此缓存可能会或可能不会被缓存。

The worst case scenario is caching does not exist, and thus your image is re-downloaded on every keyup call because $('#content').html(content); 最坏的情况是不存在缓存,因此,由于$('#content').html(content);每次键调用都将重新下载图像$('#content').html(content); empties the #content element and then re-writes the image into it. 清空#content元素,然后将图像重新写入其中。

If caching existed, you would still get repeated calls to the image, however you would not see the aggregation of downloaded data in the network inspector tool. 如果存在缓存,则仍然会重复调用该映像,但是在网络检查器工具中看不到下载数据的聚合。

Ideally your application should not be updating your document fragment images on every key press. 理想情况下,您的应用程序不应在每次按键时都更新文档片段图像。 You should find a way to target only the elements which you absolutely need updating and check your browser and server settings to ensure images can be cached correctly. 您应该找到一种方法,仅将绝对需要更新的元素作为目标,并检查浏览器和服务器设置,以确保可以正确缓存图像。

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

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