2017-07-28 9 views
1

JavaScriptの新機能の1つで、私はsessionStorageからデータを取得して、親入力がリフレッシュしていくつかの子レコードを更新した後に親入力フォームにデータを入力する必要があります。私は関数を呼び出すことができますページと関数を独立してsessionStorageからデータを取得するために、私はプロセスを組み合わせることができませんでしたので、a)ページが完全にリロードし、b)データがsessionStorageページがリロードされた後どのようにこれを行うことができますか?次のようにページリフレッシュ後にJavaScript関数の実行をどのように遅らせることができますか?

(setTimeoutメソッドを使用して機能を組み合わせるための試みで)ページ更新のための私のコードは、次のとおりのsessionStorageからデータを取得する

<script> 
function refreshPage() { 
location.reload(); 
setTimeout(GetSessionData, 4000); 
} 
</script> 

My機能は次のとおりです。

<script> 
var inputcollection = document.getElementsByTagName("input"); 

function GetSessionData() { 
    for (var i = 0; i &lt; inputcollection.length; i++) { 
    if(document.getElementById(inputcollection[i].name) != null || document.getElementById(inputcollection[i].name) != undefined) { 
    console.log(i + " " + inputcollection[i].name + "-" + inputcollection [i].value + "-"); 
    if(document.getElementById(inputcollection[i].name).type != "file"){ 
    document.getElementById(inputcollection[i].name).value = sessionStorage.getItem(inputcollection[i].name); 
    }}} 
</script> 
+0

内部のひとつはできませんsetTimeout関数と遅延を追加したい場合は....ページのリロード、後何が起こっていません走る。 – epascarello

答えて

0

をurはrefreshPage()関数の中で関数を呼び出し、ページlocation.reload()をリフレッシュするために前の命令を実行し、ページをリフレッシュし、ページの再ロードによって関数の実行が停止され、 。

ドキュメント本文のロードイベントにページリフレッシュ/ロード後にGetSessionData()関数を実行し、リフレッシュから削除する必要があります。

<html> 
    <head>  
    <!--Many things --> 
    <script> 
    function refreshPage() { 
     location.reload(); 
    } 
    </script> 
    </head> 
    <body onload="GetSessionData()"> 
    <!-- Your body code --> 
    </body> 
</html> 

あなたが別の関数に入れなければならないかGetSessionData

function GetSessionData() { 
    setTimeOut(function(){ 
     for (var i = 0; i < inputcollection.length; i++) { 
      if(document.getElementById(inputcollection[i].name) != null || 
      document.getElementById(inputcollection[i].name) != undefined) { 
       console.log(i + " " + inputcollection[i].name + "-" + 
       inputcollection [i].value + "-"); 
       if(document.getElementById(inputcollection[i].name).type != "file"){ 
        document.getElementById(inputcollection[i].name).value = sessionStorage.getItem(inputcollection[i].name); 
       } 
      } 
     } 
     },4000); 
    } 
+0

説明をいただきありがとうございました。 – DGassaway

0

あなたはこのような何か行うことができます:あなたはヨーヨーを実行していると

<script> 

    //Declare your function 
    function GetSessionData() { 
     for (var i = 0; i &lt; inputcollection.length; i++) { 
      if(document.getElementById(inputcollection[i].name) != null || document.getElementById(inputcollection[i].name) != undefined) { 
       console.log(i + " " + inputcollection[i].name + "-" + inputcollection [i].value + "-"); 
       if(document.getElementById(inputcollection[i].name).type != "file"){ 
        document.getElementById(inputcollection[i].name).value = sessionStorage.getItem(inputcollection[i].name); 
       } 
      } 
     } 
    } 
    //When the page is loaded (or reloaded in your case), set theTimeout which will call the GetSessionData, 
    document.addEventListener("DOMContentLoaded", function(event) { 
     setTimeout(GetSessionData, 4000); 
    }); 

    //Declaring your refresh function 
    function refreshPage() { 
     location.reload(); 
    } 
</script> 
+0

コードのご提案ありがとうございました – DGassaway

関連する問題