计算出执行 AJAX 请求需要多少时间

发布于 2024-10-04 15:31:38 字数 843 浏览 0 评论 0原文

我试图计算出执行 AJAX 请求需要多长时间,到目前为止,当单击按钮时,我有以下代码:

$(document).ready(function() {
    $('#start').click(function() {
        start_timer();
        var record = $.ajax({url: "ajax.php?getSensor="+devid, async: false }).responseText;
        $("textarea#recordTextbox").val($('textarea#recordTextbox').val()+record+"\n");
        stoptimer();
    }); 
});

我的计时器函数如下所示:

function start_timer() {
     display();
}
function stoptimer() {
  clearTimeout(timer);
  timer = 0;
}
function display(){
  if (millisec>=9){
     millisec=0
     seconds+=1
  }
  else
     millisec+=1
     $("#time").html(seconds + "." + millisec);
     timer = setTimeout("display()",100);
  }

AJAX 请求已正确发送,但计时器每次读取 0.1 秒,我知道它需要比这更长的时间,因为页面挂起至少 3 秒。由于某种原因,计时器在关闭 AJAX 处理时不会运行。

任何建议都会有所帮助,谢谢!

I am attempting to figure how long it takes to do an AJAX request, when a button is clicked so far I have this code:

$(document).ready(function() {
    $('#start').click(function() {
        start_timer();
        var record = $.ajax({url: "ajax.php?getSensor="+devid, async: false }).responseText;
        $("textarea#recordTextbox").val($('textarea#recordTextbox').val()+record+"\n");
        stoptimer();
    }); 
});

And my timer functions look like this:

function start_timer() {
     display();
}
function stoptimer() {
  clearTimeout(timer);
  timer = 0;
}
function display(){
  if (millisec>=9){
     millisec=0
     seconds+=1
  }
  else
     millisec+=1
     $("#time").html(seconds + "." + millisec);
     timer = setTimeout("display()",100);
  }

The AJAX request is sent properly but the timer reads 0.1 seconds everytime, I know that it takes longer than this because the page hangs for at least 3 seconds. For some reason the timer does not run when its off doing its AJAX deal.

Any advice would help, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纵山崖 2024-10-11 15:31:38

您可以使用异步请求更轻松地完成此操作,如下所示:

$(document).ready(function() {
    $('#start').click(function() {
        start_timer();
        $.ajax({url: "ajax.php?getSensor="+devid, async: false, success: function(data){ 
            $("textarea#recordTextbox").val($('textarea#recordTextbox').val()+data+"\n");
            stoptimer(); 
        }});
    }); 
});

You can do this easier with an asynchronous request as such:

$(document).ready(function() {
    $('#start').click(function() {
        start_timer();
        $.ajax({url: "ajax.php?getSensor="+devid, async: false, success: function(data){ 
            $("textarea#recordTextbox").val($('textarea#recordTextbox').val()+data+"\n");
            stoptimer(); 
        }});
    }); 
});
烟火散人牵绊 2024-10-11 15:31:38

如果您这样做是为了帮助开发,请使用 firebug

If you are doing this to aid development, use firebug.

烟花肆意 2024-10-11 15:31:38

我无法让这个计时器工作,所以我使用了 Date().getTime() ,如下所示:

    var start_time = new Date().getTime();       
    $.ajax({url: "ajax.php?getSensor="+devid, async: false, success: function(data){ 
        $("textarea#recordTextbox").val($('textarea#recordTextbox').val()+data+"\n");
        request_time = new Date().getTime() - start_time; 
    }});
    $("#time").html(request_time/1000);

I couldn't get this timer to work, so I used Date().getTime() like this:

    var start_time = new Date().getTime();       
    $.ajax({url: "ajax.php?getSensor="+devid, async: false, success: function(data){ 
        $("textarea#recordTextbox").val($('textarea#recordTextbox').val()+data+"\n");
        request_time = new Date().getTime() - start_time; 
    }});
    $("#time").html(request_time/1000);
忘年祭陌 2024-10-11 15:31:38

只需运行 YSlow 即可。

对于确定页面加载和 AJAX 请求出现问题的通用方法,请使用 YSlow Firefox 插件

它会向您展示一些您从未梦想过的东西,这些东西会减慢您的代码速度。

Just run YSlow over it.

For a general purpose approach to determining what's wrong with your page loads and AJAX requests, then use the YSlow addon for Firefox.

It'll show you stuff that you've never even dreamed of that's slowing your code down.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文