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 ,即使页面完全刷新,本地的存储不变,然后新页面依照这个存储进行判断


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM