Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 设置超时函数回调静态变量_Javascript_Node.js - Fatal编程技术网

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

Javascript 设置超时函数回调静态变量,javascript,node.js,Javascript,Node.js,使用以下代码时: var x = 5; setTimeout(function() { console.log(x); }, 2500); x = 10; 输出是10,我完全理解为什么 但是,在上面的示例中,我希望输出为5(或者更具体地说,是调用set timeout函数时的x值,而不是调用回调时的值) 向我建议的一个选项是调用函数并查找数据 大概是这样的: var x = 5; var value_of_x_at_some_time = function() { return

使用以下代码时:

var x = 5;
setTimeout(function() {
    console.log(x);
}, 2500);
x = 10;
输出是10,我完全理解为什么

但是,在上面的示例中,我希望输出为5(或者更具体地说,是调用set timeout函数时的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前端来设置事件的特定时间/长度,以及一些其他信息

我可能在同一时间有两个事件,所以当我在该函数中查找它时,我需要知道使用哪一个(假设我有两个“报警”,如果您愿意,设置,一个回调需要知道如何使用blah[x],一个回调需要知道如何使用blah[x+1])

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

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);
基本上,我在nodejs中编写一个DVR应用程序,我连接到Firebase来管理持久数据,在前端连接AngularJS,这样我可以保持大部分应用程序的断开连接,我尝试使用节点调度,所以当我在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....
然后在被调用函数中搜索数组blah

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

编辑:费利克斯·克林所说的一切。请注意,虽然我们在不同级别创建函数,但最终效果是相同的-重要的一点是存在一个函数,以引入一个新的作用域,并使用一个与原始
x
断开的新变量


EDIT2:伙计们,再投票给费利克斯的答案一点,即使我最初以10秒的优势击败了他,但他现在肯定是两个答案中的佼佼者,这不公平,他只有我的投票权:D

只是针对你问题的顶部:

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

编辑:费利克斯·克林所说的一切。请注意,虽然我们在不同级别创建函数,但最终效果是相同的-重要的一点是存在一个函数,以引入一个新的作用域,并使用一个与原始
x
断开的新变量


EDIT2:伙计们,再投票给Felix的答案一点,即使我最初以10秒的优势击败了他,他现在肯定是两个答案中的佼佼者,这不公平,他只有我的投票权:D

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

另见:

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


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

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

另见:

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


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

相同的基本思想:相同的基本思想:我刚才读过关于bind的内容,但我想我对它了解得不够,这似乎符合我的期望。我以前也读过闭包,但从来没有机会使用闭包,所以我甚至没有想过。所以读了上面的生活(我想我可能也在今晚早些时候读过),我们立即调用函数,然后返回一个值为(内部)的函数由调用函数设置……我刚才读过关于bind的内容,但我想我对它还不够了解,这似乎适合我希望做的事情。我以前也读过闭包,但从来没有机会使用闭包,所以我甚至没有想过。因此,阅读上面的生活(我想我可能也在今晚早些时候读过),我们立即调用函数,然后返回一个函数,其值(内部)由调用函数设置。。。。