背景: 本來項目開發系統防掛機功能,在其餘游覽器中均可以使用。但是呢在蘋果的safair游覽器中會出現幾率失效,最後經過排查發現是蘋果的墓碑機制導致。即:此標簽頁活躍,其他標簽頁假死。然後就導致防掛機失效了。 原理: 假如當前游覽器中有3個標簽頁分別是A,B,C,每個標簽頁都有倒計時。正常情況下,每 ...
背景:
本來項目開發系統防掛機功能,在其餘游覽器中均可以使用。但是呢在蘋果的safair游覽器中會出現幾率失效,最後經過排查發現是蘋果的墓碑機制導致。即:此標簽頁活躍,其他標簽頁假死。然後就導致防掛機失效了。
原理:
假如當前游覽器中有3個標簽頁分別是A,B,C,每個標簽頁都有倒計時。正常情況下,每個標簽頁都會倒計時。但是蘋果游覽器只會有一個標簽頁A正常倒計時,其餘的B,C 倒計時不生效。
所以就需要仿墓碑機制進行開發。原理如下:
A標簽頁打開時,B和C標簽頁不活躍;
當打開其他標簽頁,ABC處於後臺時候,最近操作的一個標簽頁處於活躍;
核心邏輯代碼:
//分鐘數 var min; var timeLeft; var timer=null; var IsnewRequest = false; var StayTimer = null; function resetTimer() { backInit(); } function uuid(len, radix) { var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); var uuid = [], i; radix = radix || chars.length; if (len) { // Compact form for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix]; } else { // rfc4122, version 4 form var r; // rfc4122 requires these characters uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'; uuid[14] = '4'; // Fill in random data. At i==19 set the high bits of clock sequence as // per rfc4122, sec. 4.1.5 for (i = 0; i < 36; i++) { if (!uuid[i]) { r = 0 | Math.random()*16; uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r]; } } } return uuid.join(''); } function GetBigTime() { //判斷:如果有其他頁面打開,則賦值最大倒計時值,如果< 10 退出 //1,獲取所有的time //2,取最大的時間 //3,如果最大的時間<10 則不管; var countDownKeyList= []; var localStorageKeys = Object.keys(localStorage) for (var i=0;i<localStorageKeys.length;i++) { var item = localStorageKeys[i]; if(item.indexOf("CountDown")!=-1) { countDownKeyList.push(item); } } var countDownValueList= []; for(var i= 0; i<countDownKeyList.length;i++) { var value = localStorage.getItem(countDownKeyList[i]); countDownValueList.push(value); } //驗證最大時間 var bigTime = countDownValueList.sort().reverse()[0]; //if(Number(bigTime)>=3) if(Number(bigTime)>=3 && Number(bigTime) != 19999999) { //取運行中的倒計時最大值; window.localStorage.setItem(window.CountDown,bigTime); return false; } else { window.localStorage.removeItem(window.CountDown); return true; } } function ShowCountDown (second){ const s = second % 60; const m = Math.floor(second / 60); return `${`00${m}`.slice(-2)} : ${`00${s}`.slice(-2)}`; }; function reSetTimeoutInit() { window.localStorage.setItem("checkClose","1"); setTimeout(function(){ //1秒後執行刷新 window.localStorage.removeItem('checkClose'); }, 1000); //單位是毫秒 setTimeoutInit(); } var IsResetTime = false; function setTimeoutInit() { this.IsnewRequest = true; //預設無操作時間為15分鐘 min = 15; let value = window.localStorage.getItem("InvalidTime"); //如果取不到session值 則預設15分鐘 if(value!=null && value!=undefined) { min = Number(value); } //清理之前的倒計時 clearInterval(StayTimer); //沒有唯一標識,則生成 if(window.CountDown==''||window.CountDown==undefined) { window.CountDown = "CountDown"+this.uuid(8,2); } //設置初始倒計時 var MainTime = min * 60; window.localStorage.setItem(window.CountDown,MainTime); StayTimer = setInterval(()=>{ var nowTime = window.localStorage.getItem(window.CountDown); if(nowTime==null||nowTime=="") { clearInterval(StayTimer); return; } //獲取存儲倒計時 timeLeft = Number(nowTime); var localStorageKeys = Object.keys(localStorage) var labelList = localStorageKeys.filter(x=> x.indexOf("CountDown")!=-1 ); var activeCount = 0; for(var i= 0; i<labelList.length;i++) { var value = localStorage.getItem(labelList[i]); var nValue = Number.parseInt(value); if(nValue!=19999999) { activeCount++; } } var localStorageKeys = Object.keys(localStorage) var labelList = localStorageKeys.filter(x=> x.indexOf("CountDown")!=-1 ); var activeCount = 0; for(var i= 0; i<labelList.length;i++) { var value = localStorage.getItem(labelList[i]); var nValue = Number.parseInt(value); if(nValue!=19999999) { activeCount++; } } //如果沒有一個活躍,則重新讀取 if(activeCount==0) { this.IsnewRequest = true; } if(this.IsnewRequest==true) { this.IsnewRequest = false; for(var i= 0; i<labelList.length;i++) { if(labelList[i]!=window.CountDown) { var value = localStorage.getItem(labelList[i]) localStorage.setItem(labelList[i],"19999999"); } else { timeLeft = MainTime;//設置的預設值 localStorage.setItem(labelList[i],MainTime); } } } if (document.hidden) { //首先重置時間 //賦值其餘的為-999 if(timeLeft==19999999) { IsResetTime = true const show = ShowCountDown(timeLeft);//恢復倒計時 並且重置時間; console.log("[時停],倒計時:"+timeLeft); } else { if(IsResetTime) { timeLeft = MainTime;//設置的預設值 IsResetTime = false; } const show = ShowCountDown(timeLeft--);//恢復倒計時 並且重置時間; console.log("[獨苗],倒計時:"+timeLeft); } } else { //首先重置時間 //賦值其餘的為-999 if(timeLeft==19999999) { IsResetTime = true const show = ShowCountDown(timeLeft);//恢復倒計時 並且重置時間; console.log("[時停],倒計時:"+timeLeft); } else { if(IsResetTime) { timeLeft = MainTime;//設置的預設值 IsResetTime = false; } const show = ShowCountDown(timeLeft--);//恢復倒計時 並且重置時間; console.log("[獨苗],倒計時:"+timeLeft); } } var checkShow = window.localStorage.getItem('checkShow'); var blCheckShow =Number(checkShow); if(blCheckShow==1) { timeLeft = -1; setTimeout(function(){ //1秒後執行刷新 window.localStorage.removeItem('checkShow'); }, 1000); //單位是毫秒 } //const show = ShowCountDown(timeLeft--); //設置倒計時 window.localStorage.setItem(window.CountDown,timeLeft); if (timeLeft < 0) { //#endregion window.localStorage.setItem('checkShow',"1"); var IsResetTime = false; var blresult = this.GetBigTime(); if(blresult==true) { //清理定時 clearInterval(StayTimer); //業務操作: document.getElementsByClassName("fullScreenDiv")[0].style.display = "block"; document.getElementsByClassName("promptDiv")[0].style.display = "block"; startCountDown($("#spanCountDown").text()); } } },1000); //countTime(); var vdoc = document.getElementById("ScormIframe").contentDocument; if (vdoc != null) { vdoc.addEventListener("mousemove", resetTimer, false); vdoc.addEventListener("mousedown", resetTimer, false); vdoc.addEventListener("keypress", resetTimer, false); vdoc.addEventListener("DOMMouseScroll", resetTimer, false); vdoc.addEventListener("mousewheel", resetTimer, false); vdoc.addEventListener("touchmove", resetTimer, false); vdoc.addEventListener("MSPointerMove", resetTimer, false); } else{ } if (language == "zh-CN") { $("#msgInfo").html("由於長時間未操作,課件將在倒計時結束後自動關閉,系統會保存您的學習記錄。"); $("#msgTimeout").html("如果您想繼續學習,請關閉此視窗。"); } else { $("#msgInfo").html("Since there is no operation for a period of time, the courseware will be closed automatically at the end of the countdown and the learning record will be saved."); $("#msgTimeout").html("If you want to continue learning, just close this window."); } } function countTime() { if (timeLeft == 0) { document.getElementsByClassName("fullScreenDiv")[0].style.display = "block"; document.getElementsByClassName("promptDiv")[0].style.display = "block"; startCountDown($("#spanCountDown").text()); } var startMinutes = parseInt(timeLeft / (60 * 1000), 10); var startSec = parseInt((timeLeft - startMinutes * 60 * 1000) / 1000) timeLeft = timeLeft - 1000; setTimeout('countTime()', 1000); //console.log(timeLeft); } function startCountDown(html){ clearInterval(this.CountDownTimer); this.CountDownTimer = setInterval(() => { var checkClose = window.localStorage.getItem('checkClose'); var blCheckClose =Number(checkClose); if(blCheckClose==1) { // $("#spanCountDown").text(60); // clearInterval(this.CountDownTimer); document.getElementsByClassName("fullScreenDiv")[0].style.display = "none"; document.getElementsByClassName("promptDiv")[0].style.display = "none"; setTimeoutInit(); setTimeout(function(){ //1秒後執行刷新 window.localStorage.removeItem('checkClose'); }, 1000); //單位是毫秒 } else { html = parseInt(html) - 1; if (parseInt(html) <= 0) { closeme(); } $("#spanCountDown").text(html); } ////console.log($("#spanCountDown").text()); }, 1000); } function closeme() { var browserName = navigator.appName; if (browserName == "Netscape") { window.open('', '_parent', ''); window.close(); } else if (browserName == "Microsoft Internet Explorer") { window.opener = "whocares"; window.close(); } } function backInit() { reSetTimeoutInit(); // min = 15; // timeLeft = min * 60 * 1000; $("#spanCountDown").text(60); clearInterval(this.CountDownTimer); document.getElementsByClassName("fullScreenDiv")[0].style.display = "none"; document.getElementsByClassName("promptDiv")[0].style.display = "none"; }
結語:
其實就是按照墓碑機制進行仿照開發,此文章進行記錄以後可以在其他平臺進行此機制的開發;
從前慢,車馬慢。 一生只愛一個人。