【发布时间】:2021-10-05 08:50:36
【问题描述】:
我已成功获取倒计时时间数据,但是当多个客户端连接到服务器时,每个人的数据看起来都不一样。 (计时器发生故障并开始每秒跳过多次。)
如何确保不断更新的数据以相同的方式发送给所有客户端?
我的 Flask 路线。
@app.route("/countdown",methods=["POST"])
def countdown():
countdown_second = int(get_db()["countdown"]) # get countdown second from db
delta_t = str(datetime.timedelta(seconds=countdown_second))
change_countdown_time(countdown_second-1) #write new countdown second to db
return jsonify(delta_t),200
我的 AJAX 调用。
$('document').ready(function () {
setInterval(function () {GET_countdown()}, 1000);
});
function GET_countdown() {
$.ajax({
url: "/countdown",
method: "POST",
success: function(response) {
$("#countdown").html(response);
},
error: function(error) {
console.log(error);
},
})
}
【问题讨论】:
标签: python ajax flask real-time