简体   繁体   English

倒数计时器完成后清除(本地储存区,工作阶段储存区)吗?

[英]clear ( local storage , session storage ) after countdown timer is finished?

i have this code for countdown timer which is stored in session storage and it's working just fine but i need to add more functionality like 我有这段代码用于倒计时计时器,该代码存储在会话存储中,并且工作正常,但是我需要添加更多功能,例如

  • if the user leaves the site before countdown timer is finshed clear 如果用户在倒数计时器清零之前离开网站
    the local session 当地会议
  • clear local session when the countdown timer is finshed 倒数计时器结束后清除本地会话

index page :- 索引页:-

<!DOCTYPE html>
<html lang="">
<head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    <meta name="Author" content=""/>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="myform"></div>
<b><p>this is the index page</p></b>
    <form id="" name="myform" action="submit.html" method="post">

    </form>
<script src="timer.js"></script>
</body>
</html>

submit page :- 提交页面:-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>New Web Project</title>
    </head>
    <body>

            <h1>submit is working</h1>



    </body>
</html>

timer.js timer.js

if(sessionStorage.getItem("total_seconds")){
    var total_seconds = sessionStorage.getItem("total_seconds");
} else {
    var total_seconds = 60*1;
}
var minutes = parseInt(total_seconds/60);
var seconds = parseInt(total_seconds%60);
function countDownTimer(){
    if(seconds < 10){
        seconds= "0"+ seconds ;
    }if(minutes < 10){
        minutes= "0"+ minutes ;
    }

    document.getElementById("myform").innerHTML = "الزمن المتبقى "+seconds+" : "+minutes;
    if(total_seconds <= 0){
        setTimeout("document.myform.submit()",1);

    } else {
        total_seconds = total_seconds -1 ;
        minutes = parseInt(total_seconds/60);
        seconds = parseInt(total_seconds%60);
        sessionStorage.setItem("total_seconds",total_seconds)
        setTimeout("countDownTimer()",1000);
    }
}
setTimeout("countDownTimer()",1000);

You can do something like this 你可以做这样的事情

function clearCountdown() {
    sessionStorage.removeItem("total_seconds")
};

window.onunload = clearCountdown();

And in your function update the if as 并在您的函数中更新if as

if(total_seconds <= 0){
        clearCountdown();
        setTimeout("document.myform.submit()",1);
Session Storage
===============

// Save data to sessionStorage
sessionStorage.setItem('key', 'value');

// Get saved data from sessionStorage
var data = sessionStorage.getItem('key');

// Remove saved data from sessionStorage
sessionStorage.removeItem('key');

// Remove all saved data from sessionStorage
sessionStorage.clear();


//Clear storage after specific time
var logoutTimer = setTimeout(function() { sessionStorage.clear(); }, (60 * 60 * 1000)); // 24 hours

Local Storage
===============
// Save data to localStorage
localStorage.setItem('key', 'value');

//Clear storage after specific time
var logoutTimer = setTimeout(function() { localStorage.clear(); }, (60 * 60 * 1000)); // 24 hours

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM