如何来ping在JavaScript或jQuery的?ping、JavaScript、jQuery

由网友(陪你不孤单)分享简介:我想创建一个游戏般平在Javascript中,就像游戏反恐精英为例。我做一个AJAX调用服务器(MySQL的),并要计算是采取的时间,但我无论是计算错了或者有侦测错误的想法。这里是code我到目前为止有:I want to create a game-like ping in Javascript, just lik...

我想创建一个游戏般平在Javascript中,就像游戏反恐精英为例。我做一个AJAX调用服务器(MySQL的),并要计算是采取的时间,但我无论是计算错了或者有侦测错误的想法。这里是code我到目前为止有:

I want to create a game-like ping in Javascript, just like the game Counter Strike for example. I'm doing an AJAX call to the server (MySQL) and want to calculate the time that's taken, but I'm either calculating it wrong or have the wrong idea of pinging. Here is the code I have so far:

var time_stamp = new Date;

$.ajax({ type: "POST",
    url: "server.php",
    data: {....},
    success: function(output){ 

        ping = new Date - time_stamp;

    }
}); // btw, this code works fine now for ping

现在的问题是,有时我得到0毫秒或3毫秒。这样行吗?这似乎非常快的去 server.php ,连接到数据库,选择一些行,并返回一些数据。是的,这是在本地主机上,因此它应该是快,但它意味着快是这个?我应该计算它的FPS,或只是每次调用 server.php

The problem is that sometimes I get 0ms or 3ms. Is this okay? It seems very fast to go to server.php, connect to database, select some rows, and return some data. Yes, this is on localhost, so it should be fast, but is it meant to be this fast? Should I be calculating it at FPS, or just each call to server.php?

推荐答案

较低的响应时间,因为默认情况下,缓存属性设置为true,将其设置为让每一个它进入服务器时不会高速缓存

the lower response time is because by default the cache property is set to true, set it to false so that every time it goes to the server not the cache

var ping = new Date;

$.ajax({ type: "POST",
    url: "server.php",
    data: {....},
    cache:false,
    success: function(output){ 

        ping = new Date - ping;

    }
});
阅读全文

相关推荐

最新文章