简体   繁体   English

javascript变量被多次调用函数覆盖

[英]javascript variable being overwritten by multiple calls to function

I have the following function which checks for the presence of a file. 我具有以下用于检查文件是否存在的功能。 It's called 4 times, one after the other, checking for different files. 它被称为4次,一次又一次地检查不同的文件。 It works fine for the first file, but doesn't work for subsequent ones. 对于第一个文件,它工作正常,但对后续文件不起作用。 I think it must be because of the position of local / global variables, but can't figure it out... any ideas? 我认为这一定是因为局部/全局变量的位置,但无法弄清楚……有什么想法吗?

 <script>

function checkfile(fileNamePassed){

    var timerForLoadingResult=setInterval( function() { checkServerForFile(fileNamePassed); }, 4000 );
    function checkServerForFile(fileNamePassed) {    
          $.ajax({
                    url: 'https://myserver.com/'+fileNamePassed+'.txt',
                    type:'HEAD',
                    error: function()
                    {

                    },
                    success: function()
                    {
                        $("#"+fileNamePassed).hide();
                        clearInterval(timerForLoadingResult)  
                    }
                });
            }

}

</script>

Remove your checkServerForFile from checkfile and pass in a reference to timerForLoadingResult when calling checkServerForFile . checkfile移除checkServerForFile ,并在调用checkServerForFile时传递对timerForLoadingResult引用

I edited my answer to reflect some real data since I don't have a back-end to work with, but the concept is the same. 由于没有后端可使用,因此我编辑了答案以反映一些真实数据,但是概念是相同的。

Removing the dependency of timerForLoadingResult and passing it in as a reference forces checkServerForFile to use it's referenced value instead of them all shares the same timerForLoadingResult . 删除timerForLoadingResult的依赖关系并将其作为参考传递时,将迫使checkServerForFile使用其参考值,而不是它们都共享相同的timerForLoadingResult

You might also want to change the name of timerForLoadingResult since setInterval returns an ID . 您可能还想更改timerForLoadingResult的名称,因为setInterval返回一个ID

From the Docs : 文档中

It returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval(). 它返回一个唯一标识该间隔的间隔ID,因此您以后可以通过调用clearInterval()将其删除。

 function checkfile(idPassed) { var timerForLoadingResult = setInterval(function() { checkServerForFile(idPassed, timerForLoadingResult); }, 4000); } function checkServerForFile(idPassed, timerForLoadingResult) { $.ajax({ url: 'https://api.github.com/users/justinjmnz', error: function() { }, success: function(data) { console.log(idPassed + " found!"); // Your file was found $("#" + idPassed).append('<img src="' + data.avatar_url + '" height="50" width="50"/ >'); clearInterval(timerForLoadingResult) } }); } checkfile("foo"); checkfile("bar"); checkfile("foobar"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="foo"> </div> <div id="bar"> </div> <div id="foobar"> </div> 

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

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