angular - 如何每 1 秒重新发送一个可观察对象的最后一个值
问题描述
我正在使用 rxjs 和 firestore 创建一个角度应用程序。
我正在处理的功能需要更新项目的百分比。
因为它是资源密集型的,所以我只更新可见项目的百分比。
百分比是使用开始日期、结束日期和当前日期计算的。因此,如果我们在项目进行到一半时,百分比将显示为 50%。
现在我订阅了该项目,并使用公式计算百分比并将其推送到数据库。问题是,我需要每 1 秒(或任意次数,我只需要它准确)重新计算这个百分比,以便我可以相应地更新百分比。
这是它的样子:
var date_percentage = this.get("projects","uz9o3cGSHq7q1p7utujq").map(
res=>{
if (this.date_percentage_calculator(res.data().start_date,res.data().end_date) >= 100) return 100
else if (this.date_percentage_calculator(res.data().start_date,res.data().end_date) <= 0) return 0
else return this.date_percentage_calculator(res.data().start_date,res.data().end_date)
}
).subscribe(calculated_percentage=>{
this.update_proprety("projects","uz9o3cGSHq7q1p7utujq","percentage",calculated_percentage)
})
现在项目可能不会被修改,因此更新程序可能只会被调用一次。
如何让可观察对象重新发送最新值,以便在可观察对象未收到值时更新百分比事件?
解决方案
再次感谢您的回答,不胜感激。
在玩了答案并在网上查找后,我找到了一个可以完成这项工作的解决方案。我不认为它是最佳的,但它可以满足我的需求。
这是它的样子:
var temp = new BehaviorSubject(null);
var temp = new BehaviorSubject(null);
var temp2 = this.sub_to("projects", "uz9o3cGSHq7q1p7utujq").subscribe(
res=>{
temp.next({date1:res.payload.data()["start_date"],date2:res.payload.data()["end_date"]})
}
)
temp.subscribe(
res => {
console.log(res)
setTimeout(function(){ temp.next(temp.value) }, 30000);
if (res){
this.update_proprety("projects","uz9o3cGSHq7q1p7utujq","percentage",this.date_percentage_calculator(res.date1,res.date2))
}
}
)
temp2 是可观察的项目,当项目更新时,他会更改 temp2 的值,并进行适当的修改。
现在每 30 秒 temp2 将他的值更改为他的实际值,这样我可以在项目不变的情况下更新百分比事件
推荐阅读
- javascript - 搜索缩小结果范围
- mysql - 没有公共字段时如何计算不同表中的数字?
- php - Wordpress 回显链接到 .php,找不到页面
- android - 如何通知用户有新数据可用,以便他们需要刷新?
- c# - 我试图从字符串转换为整数。显示输入格式不正确
- html - 动画背景没有出现在我希望的地方
- javascript - 如何将 NgbDate 转换为 js Date 对象?
- python-3.x - 如何在不使用 scipy 库函数的情况下在 python 中手动将图像顺时针旋转 45 度?
- php - MySQL 将两个不同的字符串视为相同
- c - 如何关闭在外部函数中打开的文件