设置超时函数回调静态变量



使用以下代码时:

var x = 5;
setTimeout(function() {
    console.log(x);
}, 2500);
x = 10;

输出是 10,我完全理解为什么。

但是,我希望在上面的示例中输出为 5(或者更具体地说,调用设置超时函数时 x 的值,而不是调用回调时的值。

向我建议的一个选项是调用一个函数并查找数据。

像这样:

var x = 5;
var value_of_x_at_some_time = function() {
    return old_value_of_x;
}
setTimeout(function() {
    console.log(value_of_x_at_some_time());
}, 2500);
old_value_of_x = x;
x = 10;

我理解这个想法,但这意味着我需要遍历一个数组并计算正确的值。这可能是正确的方式,但对我来说感觉不对。

我正在编写一些软件来管理调度事件(使用节点调度),例如,我可以有一个 AngularJS 前端来设置事件的特定时间/长度,以及它的其他一些信息。

可能同时有 2 个事件,所以当我在该函数中查找它时,我需要有一种方法来知道要使用哪一个(假设我有两个"警报",如果你愿意,设置,一个回调需要知道使用 blah[x],一个需要知道使用 blah[x] 例如)。

我可以查找当前时间并找到已经过去的最接近时间,然后标记我是否将其设置为执行任何需要的操作并且可能有效,但我想知道是否有办法将我使用的变量的当前状态包装为(匿名? )函数的一部分。

基本上我正在用nodejs编写一个DVR应用程序,我连接到Firebase来管理持久数据,而AngularJS在前端,所以我可以让大部分应用程序断开连接,我正在尝试使用node-schedule,所以当我在angular中添加记录事件时,我可以看到Firebase中的数据变化,调度事件, 当回调触发开始录制适当的节目时,我担心的是我可以同时设置两个节目录制,并且我必须正确管理录制它们,我有一个可能的想法是这样的数据结构:

var recording_event = {
    time: "1500",
    date: "01012015",
    length: "3600", //time in ms
    channel: "51",
    program: "1",
    scheduled: "true",
    recording: "false"
}
var blah[] = recording_events....

然后在调用的函数中搜索数组等等。

var lookup_value() {
    // loop through blah[] till you find the event closest to current time,
    // check if recording is true, if not
    // set the value of recording to true
    // else search for the next matching event
    // assume x is the index that matches correctly
    // finally return the event
    return blah[x];
}
setTimeout(function() {
    var temp = lookup_value();
    // set tuner to temp.channel
    // set tuner to temp.program
    // start recording for length of time temp.length
}, 2500);

但是,这似乎我正在做更多的工作,然后我需要做,在我看来,我希望只是将这些数据作为函数的一部分推送,所以基本上用下面的函数替换上面的函数:

temp = x //"newly secheduled event"
setTimeout(function() {
    // set tuner to temp.channel (value at the time of the scheduling)
    // set tuner to temp.program (value at the time of the scheduling)
    // start recording for length of time temp.length (value at the time of the scheduling)
}, 2500);

在运行时或多或少是动态的。有没有办法做到这一点?

(我也不知道这是否是一个好标题,我愿意接受建议)。

只是解决您问题的顶部:

var x = 5;
(function(currentX) {
    setTimeout(function () {
                    console.log(currentX);
            }, 2500);
})(x);
x = 10;

将显示5

编辑:所有Felix Kling所说的话。请注意,当我们在不同级别创建一个函数时,最终效果是相同的 - 重要的一点是存在一个函数,以引入一个新范围,其中包含一个与原始x断开连接的新变量。

编辑2:伙计们,多给菲利克斯的答案投赞成票,即使我最初以 10 秒的优势击败了他,他现在肯定是两个答案中更好的,不公平,他只有我的赞成票:D

我没有

详细阅读所有内容,但我想您想创建一个新范围来捕获变量的当前值。函数创建作用域,创建和调用函数的一种简单方法是使用 IIFE:

var x = 5;
setTimeout((function(y) {
  // this function is executed immediately and passed the current value of `x` 
  return function () {
   // this is function that is passed to setTimeout
   // since every function is a closure, it has access to the IIFE parameter y
   console.log(y);
  };
}(x)), 2500);
x = 10;

另请参阅:循环内的JavaScript闭包 - 简单的实际示例

但是,还有一个更简单的选项:您可以使用.bind将特定值绑定到函数的参数:

setTimeout(function(y) {
    console.log(y);
}.bind(null, x), 2500);

.bind创建一个新函数,并将this和参数设置为传递给它的特定值。

最新更新