简体   繁体   English

Javascript帮助-减慢页面速度

[英]Javascript help - slowing page down

I'm not experienced in Javascript, so apologies if this is a basic question, but I have a js script in the head of my application that is slowing down the page loading time (eg examplepage.php). 我没有Java经验,因此很抱歉,如果这是一个基本问题,但是我的应用程序开头有一个js脚本,它在减缓页面加载时间(例如examplepage.php)。 The code is below: 代码如下:

<script type="text/javascript">
$(document).ready(function(){ 

  $("a").click(function(){

    $.get('getdata.php', function(data) {
            $('#getdata').html(data);
        });
  });

});
</script>

getdata.php runs a script that connects to another service via an API. getdata.php运行一个脚本,该脚本通过API连接到另一个服务。 On examplepage.php I have a link that once clicked activates the js script above which pulls in data from the API (via getdata.php). 在examplepage.php上,我有一个链接,单击该链接即可激活上面的js脚本,该脚本从API(通过getdata.php)中提取数据。 This feature works, but what I am finding is that on loading the page the js script runs anyway and therefore slows down the loading time of the page. 此功能有效,但是我发现在加载页面时js脚本仍然运行,因此减慢了页面的加载时间。

I don't want the script to be calling the API unnecessarily (ie. if the user doesn't click the link on examplepage.php). 我不希望脚本不必要地调用API(即,如果用户未单击examplepage.php上的链接)。 How can I stop this js script running? 如何停止此js脚本运行?

Sorry if this doesn't make sense - I'm a JS beginner! 抱歉,如果这没有道理-我是JS初学者!

Many thanks, 非常感谢,

Gregor 格里高

The script you use is semantically equivalent to this one (I've just assigned a name to the anonymous function you use for sake of clarity): 您使用的脚本在语义上与该脚本等效(为清楚起见,我刚刚为您使用的匿名函数分配了名称):

<script type="text/javascript">
function readyAction() {
    $("a").click(clickEvent);
}

function clickEvent() {
    $.get('getdata.php', function(data) {
            $('#getdata').html(data);
        }
}

$(document).ready(readyAction);
</script>

You can read it in this way: 您可以通过以下方式阅读:

$(document).ready(readyAction); means when the $('document') is full loaded ready (it is a simplification) then execute the function readyAction . 意味着当$('document')满载ready (这是一种简化),然后执行函数readyAction Note that the function is addressed (there aren't the () after his name) and not executed at this line. 请注意,该函数已寻址(在其名称后没有()),并且不在此行执行。

The readyAction function does just a thing: scan the DOM and find all the anchors $("a") and then hook to the collected elements an handler clickEvent for the onclick event. readyAction函数只做一件事情:扫描DOM并找到所有锚点$("a") ,然后将onclick事件的处理程序clickEvent钩到收集的元素上。

The function clickEvent do the XHR connection, retrieve the data from server and then fill the div (or whatever the element is) with id getdata. clickEvent函数执行XHR连接,从服务器检索数据,然后用id getdata填充div (或任何元素)。 It will be executed only when the user clicks on a link. 仅当用户单击链接时才执行。

That's all. 就这样。

You are not issuing extra unneeded XHR calls with this code. 您不会使用此代码发出多余的XHR调用。
I hope this had shed some light on the inner workings of that code. 我希望这可以为该代码的内部工作提供一些启发。

EDIT 编辑
As pointed by Quincy, maybe the DOM you want to manipulate is quite large, or there are a lot of anchor elements and this may slow down your execution, it is a rare chance but maybe it is your case. 正如Quincy指出的那样,您可能要操纵的DOM很大,或者锚点元素很多,这可能会使您的执行速度变慢,这是一次难得的机会,但也许是您的情况。 You can try to restrict the scope of search done by jquery adding an id to the link you want to animate . 您可以尝试通过jquery将ID添加到要设置动画的链接来限制搜索范围。

ie you can change $("a").click( in $("#loadpagedatalink').click( as far 'loadpagedatalink' is the id of the link you have to attach the click handler to. 即,您可以更改$("a").click($("#loadpagedatalink').click( ,“ loadpagedatalink”是您必须附加点击处理程序的链接的ID)。

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

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