栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我必须发出数以千计的提醒,有什么办法避免每分钟发出响声吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我必须发出数以千计的提醒,有什么办法避免每分钟发出响声吗?

第一件事首先,比较时间值,使用Time.Equal,Time.Before和time.After方法。比较各个组件根本不可靠:

newYork, _ := time.LoadLocation("America/New_York")t1 := time.Date(2018, 11, 8, 4, 0, 0, 0, time.UTC)t2 := t1.In(newYork)fmt.Printf("%v == %v?n", t1, t2) // 2018-11-08 04:00:00 +0000 UTC == 2018-11-07 23:00:00 -0500 EST?fmt.Println(t1.Day() == t2.Day()) // falsefmt.Println(t2.Equal(t1))         // true

https://play.golang.org/p/06RcvuI_1Ha


对于计划问题,我将使用time.Timer。

  1. 找出接下来要发出的通知
  2. 相应地设置或重置计时器
    1. 计时器触发后,转到1
    2. 如果添加了通知,请转到1
    3. 如果删除通知,则转到1

这是一个草图:

package mainimport "time"func main() {    t := time.NewTimer(0)    go func() {        for range t.C { nextTwo := db.GetNextNotifications(2) // Sanity check if time.Until(nextTwo[0].Start) > 1*time.Second {     // The timer went off early. Perhaps the notification has been     // deleted?     t.Reset(time.Until(nextTwo[0].Start))     continue } go send(nextTwo[0]) t.Reset(time.Until(nextTwo[1].Start))        }    }()    resetTimer(t) // call as required whenever a notification is added or removed}func resetTimer(t *time.Timer) {    next := db.GetNextNotification()    t.Reset(time.Until(next.Start))}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/413163.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号