gpt4 book ai didi

javascript - AJAX:人们在没有数据库的情况下在页面上实时交互?

转载 作者:行者123 更新时间:2023-11-30 07:55:07 24 4
gpt4 key购买 nike

背景

我正在尝试设计一种交互式类环境。这个房间有不同的幻灯片、聊天框和其他一些基本功能。

我的理解

为所有用户实时更新页面的可靠方法是一个人通过 ajax 将更改保存到数据库,然后所有其他用户以定时间隔(一秒)轮询服务器以检查更改...如果他们的观点得到更新。

enter image description here

我的代码

每个房间都有一个唯一的 URL... http://www.example.com/room/ajc73

用户使用这段代码滑过 clides:

showCard();

function showCard() {
$('#card-' + (cardId)).show();
}

$('#nextCard').click(function() {
nextCard();
});

$('#previousCard').click(function() {
previousCard();
});

function nextCard() {
if ($('#card-' + (cardId + 1)).length != 0) { // if there is a next card
$('#card-' + (cardId)).hide(); // hide current card
cardId++; // increment the card id
$('#card-' + (cardId)).show(); // and show the next card
location.hash = cardId;
}
}


function previousCard() {
if (cardId != 1) { // if we are not at the first card
$('#card-' + (cardId)).hide(); // hide the current card
cardId--; // decrement the card id
$('#card-' + (cardId)).show(); // and show the previous card
location.hash = cardId;
}
}

我的问题

我是否需要将数据从 user1 保存到数据库以便调用它并显示给 user2 还是有办法删除数据库部分并将更改直接推送到浏览器?

最佳答案

使用 websockets。那将是一个更好的选择。因为它是实时的,而且只是一个更简单的逻辑将帮助您获得结果。如果你不确定你是否能够使用 websockets(比如如果你使用共享主机并且你的提供商不允许这个或任何其他原因)你可以选择各种服务,如推送器(更容易理解)帮助完成您的工作,但需要付出一些代价。

关于javascript - AJAX:人们在没有数据库的情况下在页面上实时交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42132650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com