开关控制实时刷新功能,写的时候遇到俩问题
1、在layui中直接定义函数,调用会出现未定义错误
解决方法:
window.setInterval(function() {
refreshTime();
}, 5000); // 5秒刷新一次
2、定时器要清除,但是如果 clearInterval(int) ,int 必须是 int= setInterval(function() { refreshTime(); }, 5000);
按我的写法,int为局部变量,不能传递,所以用window.int来定义
https://www.cnblogs.com/May-study/p/6138572.html 关于windows属性和全部变量、函数的关系
错误写法:
最后解决方案如下
form.on('switch(realtime)', function(data) {
// if( window.int != 'undefined'){ window.clearInterval(window.int);}
if (data.elem.checked) { //开关是否开启,true或者false
function refreshTime() {
table.reload('LAY-equipmentmanage-yunxing-manage', {});
}
window.int= setInterval(function() {
refreshTime();
}, 5000); // 5秒刷新一次
}else{
if( window.int != 'undefined'){ window.clearInterval(window.int);}
}
});