2016-08-01 16 views
0

私はセッションストレージに格納されたカウントダウンタイマーのために、このコードを持って、それがうまく働いていますが、カウントダウンタイマーが明らかfinshedされる前に、私は、ユーザーがサイトを離れた場合カウントダウンタイマー終了後にクリア(ローカルストレージ、セッションストレージ)?

  • のような多くの機能を追加する必要が
    ローカルセッション
  • カウントダウンタイマーが

インデックスページをfinshedされる明確なローカルセッション: -

<!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> 

ページ提出: -

<!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

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); 

答えて

2

あなたは

if(total_seconds <= 0){ 
     clearCountdown(); 
     setTimeout("document.myform.submit()",1); 
としてこの

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

window.onunload = clearCountdown(); 

そして、あなたの機能更新中の場合のような何かを行うことができますが

+0

私が提出し、ページを送信し、インデックスページに戻るために戻るボタンを使用した場合、カウントダウンタイマーは、ページが送信された場所から続けるどのように私はそれがfinshedされていない場合でも送信ヒットをクリアすることができます – mohammed

+0

'clearCountdown (); 'あなたの提出でも – Bikas

0
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 
関連する問題