简体   繁体   English

Javascript多个setIntervals

[英]Javascript multiple setIntervals

I have a scenario where i have one setInterval based code checking for value of an ajax call return. 我有一种情况,我有一个基于setInterval的代码来检查ajax调用返回的值。 Like so: 像这样:

var processInterval = setInterval(function () {

    var processResult = getVideoStatus(data.file_name);

    console.log(processResult);

    if (processResult === "ready") {

        clearInterval(processInterval);
        //Logic

    }

}, 1000);

As for the getVideoStatus function, here it is: 至于getVideoStatus函数,它是:

var getVideoStatus = function () {

    var result = null;
    jQuery.ajax({
        url: 'someurl',
        type: 'GET',
        dataType: 'html',
        success: function (response) {

            result = response;

        }
    });

    var statusTimeout = setInterval(function () {

        if (result != null) {

            clearInterval(statusTimeout);
            alert(result);
            return result;

        }

    }, 100);


}

What i tried to do in getVideoStatus function is for it to return value only when ajax call is completed and result is not null. 我试图在getVideoStatus函数中执行的操作是使其仅在ajax调用完成且result不为null时才返回值。 I see that it does returns/alerts correct value but when i try to console.log(processResult) , it does not log anything at all. 我看到它确实返回/警告正确的值,但是当我尝试console.log(processResult) ,它根本不记录任何内容。

I think it has something to do with first interval because the getVideoStatus seems to return value alright. 我认为这与第一个间隔有关,因为getVideoStatus似乎返回的值还可以。 Is there any logic issue? 有逻辑问题吗?

The approach you have chosen is not ideal to say less. 您选择的方法并不理想。 Just use proper Promise/thenable API which is available for providing callback when data is available. 只需使用适当的Promise / thenable API,该API可在数据可用时提供回调。 In your case: 在您的情况下:

var getVideoStatus = function () {
    return jQuery.ajax({
        url: 'someurl',
        type: 'GET',
        dataType: 'html'
    });
}

getVideoStatus().then(function(result) {
    console.log(result);
});

Benefits: cleaner and more predictable code behavior. 好处:更干净,更可预测的代码行为。

Your problem is getVideoStatus does not return anything. 您的问题是getVideoStatus不返回任何内容。 So this code: 所以这段代码:

var processResult = getVideoStatus(data.file_name);

Is not working as you expect. 无法正常工作。 There is a better way to do what you want. 有一种更好的方法来做您想要的。 It is to use jQuery Deferreds like so: 像这样使用jQuery Deferreds:

var getVideoStatus = function(){
return jQuery.ajax({
    url : 'someurl',
    type: 'GET',
    dataType: 'html',
    success : function (response) {

        result =  response;

    }
  });
}

var deferred = getVideoStatus(data.file_name);

deferred.then(
  function success(data) {
    // do something here, call came back w/ data
  },
  function error() {
    // handle error
  }
);

The thing you are not getting is that an AJAX request is asynchronous. 您没有得到的是AJAX请求是异步的。 The code will not continue running step by step -- instead, the browser will run the request and then come back and you can continue running but it won't be back in the same place that you started the request. 该代码将不会一步一步地继续运行-而是,浏览器将运行请求,然后再返回,您可以继续运行,但是不会回到开始请求的位置。

You can get something like that to work but nobody who uses jQuery would do that and it is complicated -- you need to use setInterval and check the ready state of the XHR request. 您可以使类似的东西正常工作,但是使用jQuery的人都不会这样做,而且很复杂-您需要使用setInterval并检查XHR请求的就绪状态。 jQuery does all that for you. jQuery为您完成了所有这些工作。 So start with that and then go read how it works if you want to implement your own. 因此,从此开始,然后如果想实现自己的方法,请阅读其工作原理。

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

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