0

so I noticed that when you use .addEventListener() you can get its event array like this:

document.getElementById("elementID").addEventListener("click",function(event) {
 console.log(event);
});

Is there any way I can create my own function like this? I tried this before but I completely forgot how I managed to do it.

Right now I am trying

function foo(do) {
 let a = 0;
 setTimeout(do,1);
 return a;
}

foo(function(a) {
 console.log(a);
});

(results in undefined)

7
  • 3
    Just because a variable and the parameter of a function have the same name doesn't "connect" them magically. You have to pass a to do(). Commented Aug 17, 2021 at 11:25
  • 2
    pass arguments on timeout to do by adding them to the setTimeout call - like setTimeout(do,1, argument1, argument2, ...); Commented Aug 17, 2021 at 11:25
  • @Bravo Okay thank you I will try this! Commented Aug 17, 2021 at 11:27
  • 1
    don't expect a to be changed in foo though Commented Aug 17, 2021 at 11:28
  • @Bravo I tried this is it still logged undefined Commented Aug 17, 2021 at 11:29

2 Answers 2

2

You can pass extra arguments to setTimeout, but note that updating the value in the function does not propagate it back (also note do is a reserved keyword so I changed the name to fn)

function foo(fn) {
 let a = 0;
 setTimeout(fn,1,a);
 return a;
}

const result = foo(function(a) {
 console.log(a);
 a += 1
});

console.log(result);

So, you can pass an object in instead, and updating that will indeed propagate the change:

function foo(fn) {
 let a = {value:0};
 //setTimeout(fn,1,a);
 fn(a)
 return a.value;
}

const result = foo(function(a) {
 console.log(a);
 a.value += 1
});

console.log(result);

But note that I had to take out the setTimeout and replace it with a direct call to fn(a) - if I would have left that in, the function would have returned before the call to fn was made:

function foo(fn) {
 let a = {value:0};
 setTimeout(fn,1,a);
 return a.value;
}

const result = foo(function(a) {
 console.log(a);
 a.value += 1
});

console.log(result);

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

2 Comments

"function would have returned before the call to fn was made" - at which point you'd probably suggest using promises?
@evolutionxbox exactly, but I thought that was probably a step too far for this question right now. If the OP wants to see how to make this work I will happily update the answer to show it (perhaps I should anyway)
0

Okay I found a solution:

function foo(event) {
  setTimeout(function() {
    var a = 0;
    event(a);
  },1);
}

foo(function(a) {
  console.log(a);
});

logs 0

I forgot to mention that I am using code.org and coding with code.org is very very weird. It won't let you do a lot of really cool things like adding arguments for the setTimeout

2 Comments

You still won't be able to update a from inside event, nor return from the setTimeout function.
@evolutionxbox I understand that but updating the a was not my intention when I wanted to do this.

Your Answer

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