Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass and call a function as an argument in Javascript?

I'm looking to pass an anonymous function to another function, but it doesn't seem to be working as I'd like.

I've attached the code, I think it'd give you a better idea what I'd to do.

How do I successfully pass a function as an argument, and then invoke it?

<script language="javascript" type="text/javascript">
function do_work(success) {
    success;
}

do_work(function () {
    alert("hello")
});

</script>
like image 252
Tim Ferrell Avatar asked May 19 '10 18:05

Tim Ferrell


1 Answers

You have to actually call the function:

function do_work(success) {
    success();
}
like image 171
Marcel Korpel Avatar answered Nov 04 '22 04:11

Marcel Korpel