如何来ping在JavaScript或jQuery的? [英] How to ping in JavaScript or jQuery?

查看:108
本文介绍了如何来ping在JavaScript或jQuery的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个游戏般平在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;

    }
});

这篇关于如何来ping在JavaScript或jQuery的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆