weixin_33698043 2017-04-17 11:15 采纳率: 0%
浏览 7

等待,文件准备就绪

Some file is creating in backend (up to 1min). I wrote a function to check if file is ready

function check_for_file() {
    $.ajax({
    url: '/check/', // check if file exists
    success: function(data) {
        location.replace('/hello/'); // redirect to download
        return true;
    },
    failure: function(data) {
        alert('Got an error');
    }
    });
    return false
}

a = check_for_file();
}
// a= false
// while (a == false) {
//     a = check_for_file();
// }

console.log(a);

This works fine. But I need to make a loop which will check while file is not ready. What should I do?

!!!!!!!! Look at first comment for an answer

  • 写回答

1条回答 默认 最新

  • George_Fal 2017-04-17 11:21
    关注

    a = check_for_file(); will return immediately.

    You cannot run async and return a result.

    failure is not an event by the way - it is called fail or error

    Call the function afgain in the success, error/fail or done after testing file is not ready using a setTimeout IN the success/done or failure depending on how you pass "file not found yet"

    function check_for_file() {
      $.ajax({
        url: '/check/', // check if file exists
        success: function(data) {
          if (data.fileExists) { // result from server
            location.replace('/hello/'); // redirect to download
          }
          else {
            setTimeout(check_for_file,2000); // try again
          }
        },
        error: function(data) {
          alert('Got an error');
        }
      });
    }
    
    评论

    报告相同问题?

    悬赏问题

    • ¥15 【通信原理】为什么传信率不变?频带利用率为啥没有二倍
    • ¥15 CANOPEN SDO
    • ¥15 r语言数据集循环获取问题
    • ¥30 求佬们帮助,总是出bug,求佬们解决一下bug
    • ¥15 后端Java转换字符串传给前端,前端如何解析呢?
    • ¥15 psychopy(python为基础的)中引入cmd
    • ¥15 不知道怎么去做关于前端电子请柬
    • ¥15 Ubuntu22.04打开是tty界面。提示OOM
    • ¥15 存储过程或函数中的结果集类型变量如何使用。
    • ¥100 关于海信电视聚好看安装应用的问题