异步和设置超时

时间:2017-04-11 20:06:52

标签: javascript asynchronous settimeout async.js

我在使用函数中的settimeout()时遇到了一些问题。我是异步的新手。无论我尝试多少,我都无法使超时工作。我的代码很完美,所以这不是问题。我需要每10秒执行一次请求。谢谢你的帮助。

function getContent() {

function getPelicula(pelicula, donePelicula) {
            var peli = pelicula.title;

            //request id
            request({

              url: "http://api.themoviedb.org/3/search/movie?query=" + peli + "&api_key=3e2709c4c051b07326f1080b90e283b4&language=en=ES&page=1&include_adult=false",
              method: "GET",
              json: true,
            }, function(error, res, body) {
              if (error) {
                console.error('Error getPelicula: ', error);
                return;
              }
              var control = body.results.length;
              if (control > 0) {


                var year_base = pelicula.launch_year;
                var id = body.results[0].id;
                var year = body.results[0].release_date;
                var d = new Date(year);
                var year_solo = d.getFullYear();

                if (year_base == year_solo) {
                  pelicula.id = id;
                  pelicula.year_pagina = year_solo;

                }

              } else {
                pelicula.id = null;
                pelicula.year_pagina = null;
              }
              donePelicula();

            });
          }
}

2 个答案:

答案 0 :(得分:3)

要在循环中执行某些操作,请使用setInterval

UPD:

一般来说,有两种方法可以在循环中执行某些代码

1 setTimeout

var someTimer = setTimeout(function sayHello(){
    console.log("hello!");
    someTimer = setTimeout(sayHello, 2000);
}, 2000);

请注意,如果您需要,则需要someTimer变量来停止循环过程:clearTimeout(someTimer)

2 setInterval

var someIntervalTimer = setInterval(function(){
    console.log("I'm triggered by setInterval function!");
}, 2000);

调用clearInterval(someIntervalTimer)以停止循环

这两个函数都被视为全局Window变量的属性。默认情况下,以下代码有效:

var window = this;
console.log("type of setTimeout: " + typeof window.setTimeout);
console.log("type of setInterval: " + typeof window.setInterval);

答案 1 :(得分:0)

尝试将其放入另一个功能中:

          domore(pelicula,donePelicula);

          function domore(pelicula,donePelicula) {

                // 1 second
                var timeout = 1000;
                for (var i = 1; i < pelicula.length; i++) {

                    createData(pelicula[i],donePelicula,timeout);
                    timeout = timeout + 800;

                }
            }

          function createData(peli,donePelicula,timeout) {
            setTimeout(function() { getData(peli,donePelicula); }, timeout);
          }


          function getData(peli,donePelicula) {
            var txtFile = new XMLHttpRequest();
            txtFile.open("GET", "http://api.themoviedb.org/3/search/movie?query=" + peli + "&api_key=3e2709c4c051b07326f1080b90e283b4&language=en=ES&page=1&include_adult=false", true);
            txtFile.onreadystatechange = function() {
                if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
                    if (txtFile.status === 200) {  // Makes sure it's found the file.

                        allText =  txtFile.responseText;
                        domore(allText,donePelicula);

                    }
                }
            }
            txtFile.send(null);
          }