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