1

I have this code here:

var foo = 0;
setInterval(function(){foo++}, 1000);

This will increase foo by 1 every second. Is there any way to define a bar variable which will be the foo variable at that time and not changing? It sounds confusing, so here: For example:

var foo = 0;
setInterval(function(){foo++}, 1000);
setTimeout(function(){var bar = foo}, 4000) //bar = 4;

Is there anyway to keep bar at 4 and not increasing with foo? Thanks a lot!
(Sorry for bad English)

AzDeveloper
  • 333
  • 1
  • 3
  • 14
  • 2
    I'm afraid the question is still unclear. Your code already takes a snapshot of the value of `foo` as of the timeout and stores it in `bar`, which will not change (unless you change it). So...what is it you're seeing that you want to change? – T.J. Crowder Oct 03 '17 at 08:41
  • 1
    Have you tried your code? As far as I can tell it should do what you're asking for. – DBS Oct 03 '17 at 08:44
  • 2
    Possible duplicate of [Static variables in JavaScript](https://stackoverflow.com/questions/1535631/static-variables-in-javascript) – Filnor Oct 03 '17 at 08:45
  • @T.J.Crowder my situation is a bit diffrent, im working on a server, and i want everyone to have a unique id on joining, which is a number, and will increase by 1 everytime someone join. So I have a `users` variable and a `id` variable. I thought that `id` would also increase everytime someone join (i use id= users). But it worked noe, only needed to change stuff a bit. Thanks anyway! – AzDeveloper Oct 03 '17 at 08:55
  • @AzDeveloper: Ah. Yes, it's a fairly common way to do a unique value (unique only for the duration of that run of the server; naturally, restarting the server resets the values). E.g., `var lastId = 0;` and then when you need an id: `var thisId = ++lastId;` – T.J. Crowder Oct 03 '17 at 09:12

2 Answers2

3

If you mean you want bar to be available in the same scope as foo, just move its declaration:

var foo = 0;
var bar;
setInterval(function() {
    foo++;
}, 1000);
setTimeout(function() {
    bar = foo;
}, 4000);

Once it gets that value assigned by the timer, it won't change again (unless you change it somewhere).

Live Example:

var foo = 0;
var bar;
setInterval(function() {
    foo++;
}, 1000);
setTimeout(function() {
    bar = foo;
}, 4000);
var stop = Date.now() + 10000;
var timer = setInterval(function() {
    console.log("foo = " + foo + ", bar = " + bar);
    if (Date.now() > stop) {
        clearInterval(timer);
    }
}, 1000);
.as-console-wrapper {
  max-height: 100% !important;
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

Your code already does exactly what you describe. A small change is required to make bar accessible outside of the function where it is initialized, but if you wait 6 seconds you'll see the result you expected in the example below.

var foo = 0;
var bar;
setInterval(function(){foo++}, 1000);
setTimeout(function(){bar = foo}, 4000) //bar = 4;
setTimeout(function(){
    console.log(foo,bar)
}, 6000)
Jamiec
  • 133,658
  • 13
  • 134
  • 193