js 页面定时刷新

html

           <label class="am-switch am-switch-lg">
                <input type="checkbox" id="check_is_f">
                <span class="am-switch-checkbox"></span>
            </label>
            定时刷新

js

//定时刷新
is_f = localStorage.getItem("is_f");
if(is_f == 2){
    $('#check_is_f').attr("checked", true);
    tf = setTimeout('myrefresh()',5000); //指定秒刷新一次
}




$('.am-switch input[type=checkbox]').on('click',function(){
    if($(this).is(':checked')){
        localStorage.setItem("is_f",2);
        tf = setTimeout('myrefresh()',5000); //指定秒刷新一次
    }else{
        clearTimeout(tf); //取消自动刷新
        localStorage.setItem("is_f",0);
    }
})

function myrefresh(){
window.location.reload();
}

效果:复选框勾中,页面开始每五秒刷新,取消勾中,刷新停止

主要是利用了本地存储 localStorage ,即使页面完全刷新,本地的存储不变,然后新页面依照这个存储进行判断

原文地址:https://www.cnblogs.com/tingfengqieyu/p/6704993.html