Ad

Call Function When Another Is Called

- 1 answer

I have a function, for example, boot:

function boot(){
    return("Arbitrary value");
}

Like you can listen for the onload, onclick, onkeydown, etc., is there any easy way (Without modifying the boot function) to do a sort of onboot?

[EDIT]

Seeing confusion, I will explain more.

Imagine a script that I have no control over is imported. The boot function is randomly called, and when the boot function is called, the script I have no control over wants to do something. Whoever wrote the script would like to do something like this: onboot = function(){consoe.log("whatever")}

Is this something that is possible?

Thanks, -Dylan

Ad

Answer

See this documentation. My solution creates a custom event, which will be triggered by the calling function.

Let's say we have functions base and called, base calling called once complete:

var onboot = new Event('onboot');

document.addEventListener('onboot' called);

function base () { 
  // I do stuff 
  console.log("base");
  document.dispatchEvent(onboot);
}

function called () {
  // do stuff here 
  console.log("called");
}

So if you call base now, you'll get this result:

enter image description here

Ad
source: stackoverflow.com
Ad