要解决这个问题,我们可以在后端从请求到响应之间记录下时间戳,然后在响应中返回时间戳,前端再通过时间戳来计算时间差异。这里给出一个简单的示例代码:
后端:
import time
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def hello_world():
start_time = time.time()
# 处理请求的代码
end_time = time.time()
return jsonify(response_time=end_time-start_time)
前端:
fetch('http://example.com')
.then(response => response.json())
.then(data => {
const responseTime = data.response_time; // 获取响应时间戳
const currentTime = Date.now(); // 获取当前时间戳
const timeDifference = (currentTime - responseTime) / 1000; // 计算时间差异,单位为秒
console.log(`时间差异为${timeDifference}秒`);
});
上一篇:不同像素宽度的CSS查询未被应用
下一篇:不同响应式屏幕上的图像元素移动