layui table tr a標籤倒數計時 操作 重新整理以後繼續倒數計時

尘梦發表於2024-05-21

結合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);
}

相關文章