Empty function inside another to run script from another file

Asked

Viewed 94 times

-1

I have a file called funcoes.js and another one called eventos.js need the script that is written inside events.js to be executed inside functions.js in another function, but in the order that it is placed inside it.

ex. Function within functions.js

$.fn.test = function(dados) {
  function antes(script){}
  alert(dados);
  function depois(script){}
}

script fired from events.js

$("#botao").test("teste")
antes(alert("antes");)
depois(alert("depois");)
  • You want to create the jquery function "test" and also the function "before" and "after", and when running the script "events" first it will run "test" which has "before" and "after" and then run again "before" and "after"?

1 answer

0

If I understand correctly, you want a function test which receives a parameter, but which also receives 2 snippets of code to execute before and after processing this parameter, right?

If so one way to solve it is to change its function test to receive 3 parameters: the data, the script to be executed before, and the script to be executed after:

$.fn.test = function(dados, scriptAntes, scriptDepois) {
  scriptAntes();
  alert(dados);
  scriptDepois();
}

To call the function, you pass the data normally, but each script must be a function:

$("#botao").test("teste", 
  function() { alert("antes"); }, 
  function () { alert("depois"); })

With this, the alert with "before", then "test" will appear, and finally "after".

  • i need the scripts inside the function that will be written inside the Events.js file to be executed in a certain location inside another function that is in another file in case the so-called functions.js

  • has a function called functions.js and other events.js

Browser other questions tagged

You are not signed in. Login or sign up in order to post.