php - 如何跟踪倒数计时器并存储时间以备后用
问题描述
我试图弄清楚是否有比我的方法更好的方法来跟踪和存储计时器的结果,无论是每秒还是每 X 秒,这样当用户再次返回页面时,可以再次使用这些数据并拿起它离开的地方。
我的方法是运行一个使用 AJAX 运行查询的循环 setTimeout()。我将它设置为每 5 秒执行一次,它可以工作,但我担心未来的性能,数百名用户几乎同时每 5 秒生成一个查询,我也不喜欢看到控制台被所有这些归档POST 事件,因为我不知道用户将如何解释它。
无论如何,这就是我的代码的样子。
//This setTimeout() is not the loop. This only generates a 5 seconds delay between a previous action and the recurring update and it only performs the delay once. This is for a personal need.
setTimeout(function(){
duration = getSeconds(); //this gets the current seconds listed on the timer
console.log(duration);
runUpdateProgress(c_id, id, 0, duration, 0, 1); //this passes the parameters to the function that does the update in the database
}, 5000);
//Here's the function that runs the setTimeout() loop and performs the update ever x seconds.
function runUpdateProgress(c_id, id, progress, duration, status, action){
var func = setTimeout(function start() {
//console.log({c_id, id, progress, duration, status, action});
if(action == 1){
duration = getStopWatchSeconds();
updateSaveLessonProgress(c_id, id, progress, duration, status, action);
setTimeout(start, 5000);
}else{
clearTimeout(func);
}
}, 5000);
}
该代码有效,它更新数据库并保持存储的时间,并且我可以在需要时获取上次存储的时间(可能会损失 5-8 秒,但没关系)。
我只是想知道当有大量用户触发此查询时,是否有更好的方法来执行此操作而无需敲击数据库。我还不确定这是否真的会影响现场环境中的表现,但欢迎任何更好的想法。
解决方案
推荐阅读
- python - Python:如何在多个函数中编辑(全局)变量?
- jquery - jQuery简单的下拉菜单
- javascript - Vue 可重用的模态包装器
- excel - 如何在 vb 中的每个非空单元格之后在 excel 中添加预定义的行
- javascript - 如何使用javascript将字符串转换为对象并在snake_case中返回一些键?
- javascript - 无法从 chrome 扩展中的后台脚本向内容脚本发送消息
- python - 将numpy数组转换为数据框
- python - Python 异步 grpc 客户端流句柄
- javascript - 如何禁用其他选项并在多个下拉列表中附加值与角度js中的相同值?
- python - 如何处理GitHub中的SQL数据库