layui table tr a标签倒计时 操作 刷新以后继续倒计时

结合localStorage

//开始计时

        startTimers=function(rowId,times){
            var buutons=$('#table-admin').next().find('tr[data-index="'+rowId+'"]').find("a[lay-event='send_datas']");
            buutons.addClass("layui-btn-disabled disabled-link");

            var countDown=times || 120;

            

            var timer = setInterval(function() {
                    countDown--;
                  
                    buutons.text("发送通知信息"+countDown)
                    if (countDown <= 0) {
                        localStorage.removeItem('timer_button_'+rowId)
                        buutons.removeClass("layui-btn-disabled").removeClass('disabled-link');
                        clearInterval(timer); // 倒计时结束,清除计时器
                        buutons.text("发送通知信息");
                        //取消数组
                        var table_send_index = JSON.parse(localStorage.getItem('table_send_index')) || [];
                        var indexid = table_send_index.indexOf(rowId);
                        if (indexid > -1) {
                            table_send_index.splice(indexid, 1);
                            localStorage.setItem('table_send_index', JSON.stringify(table_send_index));
                        }
                    }else {
                     
                        localStorage.setItem('timer_button_'+rowId,countDown);
                    }

                  

                }, 1000);
        }

···
...js
初始化加载
写在table的done里面
  var table_sends= JSON.parse(localStorage.getItem('table_send_index')) || [];

                for(var i=0;i<table_sends.length;i++) {
                    var timerss=localStorage.getItem('timer_button_'+table_sends[i]);
                    if (timerss) {
                        startTimers(table_sends[i],parseInt(timerss))
                    }
                }
...

事件加载
```js
 if (layEvent == 'send_datas') {

                //startTimers
                

                var data_index=$(tr).attr("data-index");

                //定义总的数据组
                var table_send_index= JSON.parse(localStorage.getItem('table_send_index')) || [];
                table_send_index.push(data_index);
                localStorage.setItem('table_send_index', JSON.stringify(table_send_index));
            

                startTimers(data_index,120);
}
posted @ 2024-05-21 10:06  尘梦  阅读(60)  评论(0编辑  收藏  举报