基於jQuery+Cookie實現的防止重新整理的線上考試倒計時,jquerycookie

來源:互聯網
上載者:User

基於jQuery+Cookie實現的防止重新整理的線上考試倒計時,jquerycookie

基於jQuery+Cookie實現的防止重新整理的線上考試倒計時

$(function() {      var _minute = parseInt("${exampaper.paperTime }");      var _expiresHours = _minute * 60 * 1000;              if(!hasSetCookie()){        addCookie("${examinee.examineeId}", _expiresHours, _expiresHours);      }       settime($("#remainTime"));     });    function hasSetCookie(){      var strCookie = document.cookie;      var arrCookie = strCookie.split("; ");      for (var i = 0; i < arrCookie.length; i++) {        var arr = arrCookie[i].split("=");        if (arr[0] == "${examinee.examineeId}") {          return true;        }      };      return false;    }    //開始倒計時    function settime(remainTime) {      var _time = getCookieValue("871d31bacfd4451484c5f70f8860c2a9");      var _countdown = parseInt(getCookieValue("${examinee.examineeId}")) / 1000;             if (_countdown <= 0) {        alert("考試時間到!");        endExam();      } else {        var _second = _countdown % 60;        var _minute = parseInt(_countdown / 60) % 60;        var _hour = parseInt(parseInt(_countdown / 60) / 60);         if (_hour < 10)          _hour = "0" + _hour.toString();        if (_second < 10)          _second = "0" + _second.toString();        if (_minute < 10)          _minute = "0" + _minute.toString();         remainTime.html(_hour + ":" + _minute + ":" + _second);        _countdown--;        editCookie("${examinee.examineeId}", _countdown * 1000, _countdown * 1000);      }      //每1000毫秒執行一次      setTimeout(function() {        settime(remainTime);      }, 1000);    };     //時添加cookie    function addCookie(name, value, expiresHours) {      var cookieString = name + "=" + escape(value); //escape() 函數可對字串進行編碼,這樣就可以在所有的電腦上讀取該字串。      //判斷是否設定到期時間,0代表關閉瀏覽器時失效      if (expiresHours > 0) {        var date = new Date();        date.setTime(date.getTime() + expiresHours * 1000);        cookieString = cookieString + ";expires=" + date.toUTCString();      }      document.cookie = cookieString;    }     //修改cookie的值    function editCookie(name, value, expiresHours) {      var cookieString = name + "=" + escape(value);      if (expiresHours > 0) {        var date = new Date();        date.setTime(date.getTime() + expiresHours * 1000); //單位是毫秒        cookieString = cookieString + ";expires=" + date.toGMTString();      }      document.cookie = cookieString;    }     //根據名字擷取cookie的值    function getCookieValue(name) {      var strCookie = document.cookie;      var arrCookie = strCookie.split("; ");      for (var i = 0; i < arrCookie.length; i++) {        var arr = arrCookie[i].split("=");        if (arr[0] == name) {          return unescape(arr[1]);          break;        } else {          continue;        };      };    }

以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.