firebase - Firebase 动态触发某些东西的正确方法
问题描述
让我首先用一个例子来描述你我想做什么:
- Players join a game (it's a web turn-based app/game with firebase/angular)
- When they are ready they have 30 seconds to choose a character
- Then player 1 has 30 sec to end his turn
- Then player 2 has 30 sec to end his turn
- ...
这是一个小回合制游戏,我很难结束一个回合。我考虑过在云函数上使用 cron 或 setInterval,但不建议使用 setInterval(成本高昂),而且 cron 也不是我想要的(我不想每天触发某些事情……)。我在两个相似的主题上看到了这些信息:
setInterval on firebase 而不是使用 cron && Cloud Functions for Firebase 按时触发?
正确的方法应该是什么(根据时间结束回合以增加游戏的“状态”)?
解决方案
您可以在客户端 js 中使用 setInterval。在浏览器中运行 setInterval 是在 javascript 中预先确定的时间间隔后执行某些操作的正确方法。setInterval 不适用于云功能,但您不需要它们。
伪代码如下:
- 通过保存当前时间开始玩家的回合
- setInterval 开始时间 + 间隔
- 30 秒后,您的 setInterval 回调执行,您可以在其中更新游戏状态
- 冲洗并重复
推荐阅读
- powerpoint - 彼此相邻的 Powerpoint 堆叠列
- jmeter - 无法获取 jmeter 中 prev.getParent().getStartTime() 的响应时间
- apache-kafka - 如何在 Storm 上制作同步 KafkaSpout
- python - 当我到达包含对象的屏幕时,如何使用 ScreenManager 中的小部件进行移动?
- arrays - 使用 Excel VBA 评估一维数组中可变长度字符串的最大长度
- bash - 在 BASH 的变量中循环由 `*` 定义的文件
- excel - 将文本附加到单元格但卡在循环中
- javascript - Adonis.js 验证 request.multipart.file 图像大小和扩展名
- node.js - NodeJS 和 Socket.io 为两个用户创建房间
- python - 输入一个复数并将其作为 str 返回