第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

JavaScript 定時(shí)器和 sessionStorage

JavaScript 定時(shí)器和 sessionStorage

慕容3067478 2023-09-21 16:27:42
我有以下問(wèn)題。我有一個(gè)計(jì)時(shí)器函數(shù),它需要一個(gè)參數(shù),即秒數(shù)。直接通過(guò)的話,就可以正常工作,沒(méi)有任何缺陷。但是,出于多種原因,我需要使用 sessionStorage,并且當(dāng)我在將變量傳遞給計(jì)時(shí)器函數(shù)之前從 sessionStorage 檢索變量時(shí),它會(huì)乘以參數(shù)中傳遞的秒數(shù)的 10 倍。我不明白為什么它有這種行為。我寫(xiě)了一個(gè)最小的例子來(lái)說(shuō)明我的問(wèn)題:function countdown(time_p) {  var saved_countdown = sessionStorage.getItem('saved_countdown');  var time;  if (saved_countdown == null) {    // Set the time we're counting down to using the time allowed    var new_countdown = new Date().getTime() + (time_p + 2) * 1000;    time = new_countdown;    sessionStorage.setItem('saved_countdown', new_countdown);  } else {    time = saved_countdown;  }  // Update the count down every 1 second  var x = setInterval(() => {    // Get today's date and time    var now = new Date().getTime();    // Find the distance between now and the allowed time    var distance = time - now;    // Time counter    var counter = Math.floor(distance / 1000);    // Output the result in an element with id="demo"    document.getElementById("demo").innerHTML = counter + " s";    // If the count down is over, write some text     if (counter <= 0) {      clearInterval(x);      sessionStorage.removeItem('saved_countdown');      sessionStorage.removeItem('max_time');      document.getElementById("demo").innerHTML = "EXPIRED";    }  }, 1000);}$("#confirm_settings").click(function() {  var time = $("#time").val();  console.log("time entered" + time);  sessionStorage.setItem('max_time', time);  time = sessionStorage.getItem('max_time'); // multiply the time entered by 10 ...  // time = 42; // works well  console.log("time = " + time);  countdown(time);});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><p id="demo"> </p><label for "time"> Time limit :</label><input type="text" id="time" placeholder="enter a duration (s)"> <br><button type="button" id="confirm_settings">OK</button>
查看完整描述

1 回答

?
慕婉清6462132

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊

窗口存儲(chǔ)保存字符串。您需要將它們轉(zhuǎn)換為整數(shù):


function countdown(time_p) {

  var time = sessionStorage.getItem('saved_countdown');


  if (time) {

    time = +time; // cast the string to number

  }

  else {

    // Set the time we're counting down to using the time allowed

    var new_countdown = new Date().getTime() + (time_p + 2) * 1000;

    time = new_countdown;

    sessionStorage.setItem('saved_countdown', new_countdown);

  } 


查看完整回答
反對(duì) 回復(fù) 2023-09-21
  • 1 回答
  • 0 關(guān)注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)