js实现页面title定时闪烁

原文地址

在页面收到消息之后需要通过titile闪烁的方式来提示用户,可以通过js来实现

class MessageNotice {
    timer = undefined;
    title = document.title;
    count = 0;

    show() {
        this.timer = setInterval(() => {
            // 偶数显示提示
            if (this.count % 2 === 0) {
                document.title = "【新消息】" + this.title;
            } else {
                document.title = "【  】" + this.title;
            }
            this.count++;
        }, 500)
    }

    stop() {
        if (this.timer) {
            clearInterval(this.timer );
            this.timer = null;
            this.count = 0;
            document.title = this.title;
        }
    }
}

const messageNotice = new MessageNotice();
// 开始执行
messageNotice.show();

// 停止
//messageNotice.stop();

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值