Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
页面刷新后,如何延迟javascript函数的执行?_Javascript - Fatal编程技术网

页面刷新后,如何延迟javascript函数的执行?

页面刷新后,如何延迟javascript函数的执行?,javascript,Javascript,作为JavaScript新手,我需要从sessionStorage中检索数据,并在父输入刷新后填充父输入表单以更新一些子记录。我可以调用函数刷新页面,也可以调用函数独立地从sessionStorage获取数据,但我无法组合这些过程,以便a)页面完全重新加载,然后b)在页面重新加载后从sessionStorage检索数据。怎么能这样呢 我的页面刷新代码(尝试使用setTimeout方法组合函数)如下所示: <script> function refreshPage() { locat

作为JavaScript新手,我需要从sessionStorage中检索数据,并在父输入刷新后填充父输入表单以更新一些子记录。我可以调用函数刷新页面,也可以调用函数独立地从sessionStorage获取数据,但我无法组合这些过程,以便a)页面完全重新加载,然后b)在页面重新加载后从sessionStorage检索数据。怎么能这样呢

我的页面刷新代码(尝试使用setTimeout方法组合函数)如下所示:

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

函数刷新页面(){
location.reload();
setTimeout(GetSessionData,4000);
}
我从会话存储中获取数据的功能如下:

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

var inputcollection=document.getElementsByTagName(“输入”);
函数GetSessionData(){
对于(变量i=0;i inputcollection.length;i++){
if(document.getElementById(inputcollection[i].name)!=null | | document.getElementById(inputcollection[i].name)!=undefined){
console.log(i+“”+inputcollection[i]。名称+“-”+inputcollection[i]。值+“-”;
if(document.getElementById(inputcollection[i].name).type!=“文件”){
document.getElementById(inputcollection[i].name).value=sessionStorage.getItem(inputcollection[i].name);
}}}

您可以这样做:

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

//声明您的函数
函数GetSessionData(){
对于(变量i=0;i inputcollection.length;i++){
if(document.getElementById(inputcollection[i].name)!=null | | document.getElementById(inputcollection[i].name)!=undefined){
console.log(i+“”+inputcollection[i]。名称+“-”+inputcollection[i]。值+“-”;
if(document.getElementById(inputcollection[i].name).type!=“文件”){
document.getElementById(inputcollection[i].name).value=sessionStorage.getItem(inputcollection[i].name);
}
}
}
}
//当页面被加载(或者在您的情况下被重新加载)时,设置调用GetSessionData的时间,
document.addEventListener(“DOMContentLoaded”),函数(事件){
setTimeout(GetSessionData,4000);
});
//声明刷新函数
函数刷新页面(){
location.reload();
}

您可以这样做:

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

//声明您的函数
函数GetSessionData(){
对于(变量i=0;i inputcollection.length;i++){
if(document.getElementById(inputcollection[i].name)!=null | | document.getElementById(inputcollection[i].name)!=undefined){
console.log(i+“”+inputcollection[i]。名称+“-”+inputcollection[i]。值+“-”;
if(document.getElementById(inputcollection[i].name).type!=“文件”){
document.getElementById(inputcollection[i].name).value=sessionStorage.getItem(inputcollection[i].name);
}
}
}
}
//当页面被加载(或者在您的情况下被重新加载)时,设置调用GetSessionData的时间,
document.addEventListener(“DOMContentLoaded”),函数(事件){
setTimeout(GetSessionData,4000);
});
//声明刷新函数
函数刷新页面(){
location.reload();
}

当您在refreshPage()函数中执行函数时,它将执行上一条指令以刷新页面位置。reload(),因此它将刷新页面,函数执行因页面重新加载而停止,并且以下挂起的代码永远不会执行

您应该在页面刷新/加载之后,将GetSessionData()函数执行放在文档正文的加载事件上,并将其从刷新中删除

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

函数刷新页面(){
location.reload();
}
如果要使用setTimeOut函数添加延迟,则应将其放入另一个函数或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);
    }
函数GetSessionData(){ setTimeOut(函数(){ 对于(var i=0;i当您在refreshPage()函数中执行函数时,它将执行上一条指令以刷新页面位置。reload(),因此它将刷新页面,函数执行因页面重新加载而停止,并且以下挂起的代码永远不会执行

您应该在页面刷新/加载之后,将GetSessionData()函数执行放在文档正文的加载事件上,并将其从刷新中删除

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

函数刷新页面(){
location.reload();
}
如果要使用setTimeOut函数添加延迟,则应将其放入另一个函数或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);
    }
函数GetSessionData(){ setTimeOut(函数(){ 对于(var i=0;i