1

How do I run five JavaScript functions in one click?

Sample code:

<img alt="move right" 
     src="Pictures/sides/right.gif" 
     id="MoveRight" 
     ***onclick="javascript:moveObjRight('ground','sky','mountain');"***
     style="z-index: 1; left: 801px; top: 37px; position: absolute; height: 33px; width: 34px;" 
/>
1
  • I guess function calls are made synchronously. You can make a call only after the prev call is completed. I am not sure though. Commented Apr 26, 2011 at 5:35

4 Answers 4

4
<img alt="move right" src="Pictures/sides/right.gif" id="MoveRight"
    onclick="moveObjRight('ground','sky','mountain');myFunction2();myFunction3();myFunction4();myFunction5();"
    style="z-index: 1; left: 801px; top: 37px; position: absolute; height: 33px;
    width: 34px;" />

or

<script type="text/javascript">
window.onload = function() {
    var ele = document.getElementById('MoveRight');
    ele.onclick = function() {
        moveObjRight('ground','sky','mountain');
        myFunction2();
        myFunction3();
        myFunction4();
        myFunction5();
    }
};
</script>

Note that the second option will override any previously assigned window.onload and ele.onclickfunction. If you are using a javascript library use the appropriate dom ready event (e.g. jQuery: $(document).ready(function(){/*code*/}); or short: $(function(){/*code*/}))

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

7 Comments

@Thomas MyFunction2() will be called only after moveObjRight(..) right? Is there any way to call all the functions async?
is it possible to run functions in thread mode, i mean all functions run in one time not one by one?
not really, remember that all javascript engines (i'm aware of) run javascript synchronous - even when a event function like onclick is executed all other javascript on that page is halted.
not really, while you loose control to when it is run it will never execute 2 functions at the same time. setTimeout's real purpose is to run a function at a later time (btw. note that it is not guaranteed that it will run exactly after the specified time/milliseconds).
Javascript is single threaded, you can't run functions asynchronously. You can run xmlHTTPRequests asynchronously, but when they return, their callback functions will be placed in a queue and run in their turn. setTimeout allows functions to be run after some interval, but does not provide any kind of asynchronous behaviour.
|
4

You can add all your functions in the onclick property of the img tag

<img onclick="fnc1();fnc2();..." />

Comments

1

You can call a single method on your on click event and in that method you can call all your other methods.

Comments

0

Call multiple functions in an other function

<script type="text/javascript">
   function CallMultipleFunctions() {
     Function1();
     Function2();
     Function3();
     Function4();
}
</script>

<img onclick= "CallMultipleFunctions()"/>

EDIT:

Check this link

2 Comments

is it possible to run functions in thread mode, i mean all functions run in one time not one by one?
For all programming considerations, Javascript is single-threaded[1] However, because the web browser model is single-threaded, all four functions will run before the user gets control back. You'll need to say why you need something different before we could advise you on other solutions. [1]Yes, there are web-workers, but those can't interact with the UI so I'm ignoring them.

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.