检查任务完成 [英] check that a task is completed

查看:107
本文介绍了检查任务完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该检查任务是否完成的JavaScript函数。 当任务完成时有一个在服务器上的文件一个完成记录。 应该进行递归调用服务器的一些延迟功能(有可能增加),直到它得到的文件,在完成备案。 下面给出的code,使过多的调用服务器的间隔不到一秒钟 例如,从Web控制台:    [20:06:21.202] [20:06:21.563] [20:06:21.990] 但这个任务变得可变的 WAITTIME 价值竞争越来越等于 max_waittime 。 虽然为测试条件下的整体产量不如预期,什么是错的功能。 我哪里错了?

 函数check_status(时间,div_id,文件名){

VAR状态= 0;
VAR WAITTIME =时间;

VAR max_waittime = 11000000;
如果(WAITTIME< max_waittime){WAITTIME = WAITTIME + 1000000; }

$阿贾克斯({
  键入:GET,
  异步:假的,
  网址:code_on_server_checking_file.php
  数据:F =+文件名,
  数据类型:文本,
  成功:函数(内容){

    如果(内容){
    //东西相关的结果的输出
       ....
       返回状态= 1;
     }
    其他{返回状态= 0;}
      }
   });

如果(状态== 0安培;&安培; WAITTIME< 20000000){
   的setTimeout(check_status(WAITTIME,div_id,文件名),WAITTIME);
  }
其他{警报(check_status过去了!+状态+'|'+ WAITTIME);}
}
 

解决方案

您呼吁给予它作为一个参考的setTimeout的函数。环绕你的函数调用的匿名函数。此外,倒不如简单地建立在Ajax回调调用如果需要的话,而不是使用同步调用。同步调用会占用你的浏览器。

 函数check_status(时间,div_id,文件名){

    $阿贾克斯({
      键入:GET,
      网址:code_on_server_checking_file.php
      数据:F =+文件名,
      数据类型:文本,
      成功:函数(内容){
        如果(内容){
        //东西相关的结果的输出
        }
        其他 {
          时间+ = 1000000;
          如果(时间LT; 20000000){
              的setTimeout(函数(){check_status(时间,div_id,文件名);},时间);
          }
        }
      }
    });

}
 

I have a javascript function which supposed to check whether a task is completed. When the task is completed there is a completion record in a file on the server. The function supposed to make recursive calls to the server with some delay (potentially increasing) till it gets the completion record in the file. The code given below makes excessive calls to the server with interval less than a second example from Web Console: [20:06:21.202] [20:06:21.563] [20:06:21.990] But the task becomes competed on variable waittime value getting equal to max_waittime . Though for a test case overall output is as expected, something is wrong with the function. Where I'm wrong?

 function check_status(time,div_id,filename) {

var status =0;
var waittime=time;

var max_waittime=11000000;
if (waittime < max_waittime){waittime=waittime+1000000; }

$.ajax({
  type: "GET",
  async: false,
  url: "code_on_server_checking_file.php",
  data: "f="+filename,
  dataType: "text",
  success: function(content) {

    if (content ) {
    // stuff related to output of the result 
       ....
       return status=1;
     }
    else {return status=0;}   
      }
   });

if (status == 0 && waittime < 20000000){
   setTimeout(check_status(waittime,div_id,filename),waittime);
  }
else {alert('check_status passed!'+status+'|'+waittime);}
}

解决方案

You are calling the function instead of giving it as a reference to setTimeout. Wrap your function call in an anonymous function. Also, it would be better to simply set up the call in the ajax callback if needed rather than using a synchronous call. A synchronous call will tie up your browser.

function check_status(time,div_id,filename) {

    $.ajax({
      type: "GET",
      url: "code_on_server_checking_file.php",
      data: "f="+filename,
      dataType: "text",
      success: function(content) {
        if (content ) {
        // stuff related to output of the result 
        }
        else {
          time += 1000000;
          if (time < 20000000) {
              setTimeout( function() { check_status( time, div_id, filename); }, time );
          }
        }
      }
    });

}

这篇关于检查任务完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆