0

I'm trying to create reusable function that has settimeout function inside that will change the variable passed to the first function.

let variable = true;


reusablefunction = (passedvariable) => {


    setTimeout(() => {

        passedvariable = false;
       // this turns out true for some reason

    }, 1, passedvariable);
  };



// Change value of passed variable (variable) to false after a timeout 

reusablefunction(variable);
8
  • 1
    With how Javascript works, that can not happen. Commented Jul 30, 2019 at 16:08
  • 1
    This sounds like an X/Y problem. What is the underlying thing you're trying to solve by doing this? Commented Jul 30, 2019 at 16:10
  • Looks like it is no more an X/Y problem to you, because you answered it @T.J.Crowder .. Right? Commented Jul 30, 2019 at 16:17
  • @ArupRakshit - I answered the question asked (Y). But while that's probably a bit helpful, I suspect if we knew what the underlying issue is (X), we could probably offer a better solution for it. Commented Jul 30, 2019 at 16:20
  • I'm trying to create reusable function that will take varible, run timeout function and change the value of a said variable after some time. Im sure there are some better options to do it, I just didn't figure them out. Commented Jul 30, 2019 at 16:21

1 Answer 1

2

You can't. When you do

reusableFunction(variable);

the value of variable is passed into the function, not the variable itself. The parameter passedVariable is not in any way connected to variable. That is, you end up with something like this in memory:

+−−−−−−−−−−−−−−−−−+
| variable: false |
+−−−−−−−−−−−−−−−−−+

+−−−−−−−−−−−−−−−−−−−−−−−+
| passedVariable: false |
+−−−−−−−−−−−−−−−−−−−−−−−+

You could pass in an object and have the function update a property on it instead:

let obj = {
    prop: true
};

let reusablefunction = (passedObject) => {
    setTimeout(() => {
        passedObject.prop = false;
    }, 1); // No need to pass the parameter here
};

reusablefunction(obj);

With that, you end up with something like this in memory:

+−−−−−−−−−−−−−−−+              
| obj: Ref55461 |−−−−−−−−−−−−−+
+−−−−−−−−−−−−−−−+             |
                              |          +−−−−−−−−−−−−−+
                              +−−−−−−−−−>|   Object    |
                              |          +−−−−−−−−−−−−−+
+−−−−−−−−−−−−−−−−−−−−−−−−+    |          | prop: false |
| passedObject: Ref55461 |−−−−+          +−−−−−−−−−−−−−+
+−−−−−−−−−−−−−−−−−−−−−−−−+

Since both obj and passedObject refer to the same object, you can change its properties via either of them.


That said, there may well be a better way to solve the underlying problem you're trying to solve...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.