强制将javascript变量传递为“value”

时间:2020-12-10 00:42:12

I am having a problem with a classic javascript "issue"!

我有一个经典的javascript“问题”的问题!

I want to create an array of functions that will each be set to run after different lenghs of time (each one second apart).

我想创建一个函数数组,每个函数都将设置为在不同的时间间隔后运行(每一秒间隔)。

However the following code is creating functions that all have the timeout set to the last value of timeout.

然而,下面的代码创建的函数都将超时设置为超时的最后一个值。

Any help to solve this would be wonderful! Thanks!

任何解决这个问题的帮助都将是非常棒的!谢谢!

    var timeout = 0;
    var myfunctions = []

    urls.forEach(function(url){

      var newFunction = function(callback){
        (function (timeout){
          setTimeout(function(){
            getTransactions(url, function(err, data){
              callback(err, data)
            })
          },timeout)

        })(timeout)

      }
      timeout = timeout + 1000
      myfunctions.push(newFunction)
    })

The reason each function has a callback is that I that plan to run them all using:

每个函数都有回调的原因是我打算用以下方法运行它们:

    async.parallel(myfunctions,
      function(err, results) {
        if(err) {
          console.log(err.message)
        }
        else{
          console.log(results)
        }
      })

1 个解决方案

#1


3  

You've just misapplied the usual pattern; you're doing it too far in, waiting to call the inner IIFE until your function is called, at which point timeout is already updated.

你只是误用了通常的模式;您做得太过分了,等待调用内部IIFE,直到调用您的函数,此时超时已经更新。

But there's no need for the IIFE at all, you can use the closure of the callback to forEach:

但是根本就没有必要,你可以使用回调的闭包来forEach:

var timeout = 0;
var myfunctions = []

urls.forEach(function(url) {
    var thisTimeout = timeout;
    var newFunction = function(callback) {
        setTimeout(function() {
            getTransactions(url, function(err, data) {
                callback(err, data)
            })
        }, thisTimeout)
    };
    timeout = timeout + 1000
    myfunctions.push(newFunction)
});

Live Example:

生活例子:

var timeout = 0;
var myfunctions = [];

var urls = ["first", "second", "third"];

urls.forEach(function(url) {
    var thisTimeout = timeout;
    var newFunction = function(callback) {
        console.log("url = " + url + ", thisTimeout = " + thisTimeout);
        /*
        setTimeout(function() {
            getTransactions(url, function(err, data) {
                callback(err, data)
            })
        }, thisTimeout)
        */
    };
    timeout = timeout + 1000
    myfunctions.push(newFunction)
});

myfunctions.forEach(function(f) {
  f();
});

#1


3  

You've just misapplied the usual pattern; you're doing it too far in, waiting to call the inner IIFE until your function is called, at which point timeout is already updated.

你只是误用了通常的模式;您做得太过分了,等待调用内部IIFE,直到调用您的函数,此时超时已经更新。

But there's no need for the IIFE at all, you can use the closure of the callback to forEach:

但是根本就没有必要,你可以使用回调的闭包来forEach:

var timeout = 0;
var myfunctions = []

urls.forEach(function(url) {
    var thisTimeout = timeout;
    var newFunction = function(callback) {
        setTimeout(function() {
            getTransactions(url, function(err, data) {
                callback(err, data)
            })
        }, thisTimeout)
    };
    timeout = timeout + 1000
    myfunctions.push(newFunction)
});

Live Example:

生活例子:

var timeout = 0;
var myfunctions = [];

var urls = ["first", "second", "third"];

urls.forEach(function(url) {
    var thisTimeout = timeout;
    var newFunction = function(callback) {
        console.log("url = " + url + ", thisTimeout = " + thisTimeout);
        /*
        setTimeout(function() {
            getTransactions(url, function(err, data) {
                callback(err, data)
            })
        }, thisTimeout)
        */
    };
    timeout = timeout + 1000
    myfunctions.push(newFunction)
});

myfunctions.forEach(function(f) {
  f();
});