简体   繁体   English

for循环后重定向到不同的网页

[英]Redirect to different webpage after for loop

I'm trying to get some javascript to run a for loop which will create an xml file and then, after the loop is complete, to visit a link that would allow the user to download the file. 我正在尝试获取一些javascript来运行for循环,这将创建一个xml文件,然后,在循环完成后,访问一个允许用户下载文件的链接。 Currently what happens is that it gets half-way through creating the file and then sends the user the link to download the file. 目前发生的事情是它在创建文件的过程中获得了一半,然后向用户发送链接以下载文件。

I followed this question's advice and created a callback variable and moved the download link to another function, however that hasn't solved the problem either. 我按照这个问题的建议并创建了一个回调变量并将下载链接移动到另一个函数,但是这也没有解决问题。 Does anyone else have any idea how to do it? 有没有其他人知道怎么做? My code is: 我的代码是:

var callbackcount=0;
function populateTemplate(numResults) {
  for (i=1; i < numResults; i++) {
    $.post('linkToTheFileThatCreatesTheXMLFile', {WithSomeParameters};
    download(numResults);
  }
}

function download(numResults) {
  callbackcount++;
  if (callbackcount == numResults) {
    window.location.href="linkToPHPThatDownloadsFile";
  }
}

Thanks heaps 谢谢堆

Edit: Console.log results: 编辑: Console.log结果:

LOG: 230 LOG: 330 LOG: 230 LOG: 330 
LOG: 430 LOG: 530 LOG: 630 LOG: 730 
LOG: 830 LOG: 930 LOG: 1030 LOG: 1130 
LOG: 1230 LOG: 1330 

If numResults = 7 如果numResults = 7

LOG: 27 LOG: 37 LOG: 47 

or 要么

LOG: 27 LOG: 37 LOG: 47 LOG: 57 

etc. 等等

Edit: The new code: 编辑: 新代码:

function populateTemplate(numResults) {
  var callbackcount = 1;
  for (i=1; i < numResults; i++) {
    $.post('linkToTheFileThatCreatesTheXMLFile', {WithSomeParameters}, function() {
      callbackcount++;
      console.log(callbackcount, numResults);
      if(callbackcount === numResults - 1) {
        window.location.href="linkToPHPThatDownloadsFile";
      }
    });
  }
}

$.post is asynchronous call. $.post是异步调用。 So by the time you get to the last loop, where in the closure callbackcount will equal numResults all the posts won't have gone through yet. 所以当你到达最后一个循环时,封闭中callbackcount将等于numResults所有帖子都不会通过。 definitly not the last one. 绝对不是最后一个。

$.post allows for a callback which is called once the post is complete. $.post允许在帖子完成后调用的回调。 This callback should call your download methods. 此回调应调用您的download方法。

var callbackcount=0;
function populateTemplate(numResults) {
  for (i=1; i < numResults; i++) {
    $.post('linkToTheFileThatCreatesTheXMLFile', {WithSomeParameters}, function() {

      callbackcount++;
      if (callbackcount === numResults) {
        window.location.href="linkToPHPThatDownloadsFile";
      }

    });
  }
}

This way you evaluate the function when ever the post is complete. 这样,您可以在帖子完成时评估函数。 Also I inlined it to make the access of the variables a little esier. 此外,我还介绍了它使变量的访问更加集中。

BTW: always use === over == in JavaScript See why BTW:总是在JavaScript中使用=== over == 看看为什么

EDIT 编辑

The loop index i should start at 0. Also it should be declared as a local variable, not global. 循环索引i应该从0开始。它也应该声明为局部变量,而不是全局变量。

for ( var i = 0; i < numResults; i++ )

Otherwise the loop will only run 5 times with a numResults of 6. 否则循环将仅运行5次, numResults为6。

EDIT 2 - Second approach 编辑2 - 第二种方法

If it has to do with too many requests per frame (which I doubt) You could also try this and see if it makes a difference. 如果它与每帧的请求太多(我怀疑)你也可以尝试这个,看看它是否有所作为。

var callbackcount=0;
function populateTemplate(numResults) {
  for (i=1; i < numResults; i++) {

    setTimeout( function() {

      $.post('linkToTheFileThatCreatesTheXMLFile', {WithSomeParameters}, function() {

        callbackcount++;
          if (callbackcount === numResults) {
            window.location.href="linkToPHPThatDownloadsFile";
          }

      });

    }, 100);
  }
}

this delays every request and therefore has not every request going in one frame. 这会延迟每个请求,因此不是每个请求都在一帧中。

It looks like you are using jQuery. 看起来你正在使用jQuery。 The code you posted is incorrect, it should be this: 您发布的代码不正确,应该是这样的:

$.post( 'linkToTheFileThatCreatesTheXMLFile', {Parameters}, function() {
    download(numResults);
});

(Unless your code formatting just got messed up) (除非你的代码格式搞砸了)

also, make sure numResults is defined and initialized up by callbackcount. 另外,确保numResults由callbackcount定义和初始化。 (might be a scoping issue) (可能是一个范围问题)

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

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