Skip to content Skip to sidebar Skip to footer

Making A Function To Wait An Event Before Returning?

function myFunction() { wait(); //what I put there? return; } myFunction(); //this is an event; when its triggered I want function to resume onSomething = function() {

Solution 1:

This is not possible.

Instead of waiting for the event, you want to put whatever you need to do when it happens into the onSomething function.

Solution 2:

You have to switch your brains to thinking in events instead of the "traditional" programming flow. The way to do this is:

functionmyFunctionPart1() {
    doSomething();
}

functionmyFunctionPart2() {
    doSomethingElse();
}

myFunctionPart1();

onSomething = function() {
    myFunctionPart2();
}

Solution 3:

So, with a generator it would look like so:

functiontest() {
    // first part of functionyield;
    // second part of functionyield;
}

var gen = test(); // creating a generator

gen.next(); // execute first part

button.onclick = function () {
    gen.next(); // execute second part on button click
};

Live demo:http://jsfiddle.net/MQ9PT/

This however doesn't work beyond Firefox. It will become part of the ECMAScript standard in the next edition...

Post a Comment for "Making A Function To Wait An Event Before Returning?"