Search code examples
javascriptphase

How do I do phase shifting with javaScript?


Why isn't the two phases shifting in the fiddle: http://jsfiddle.net/WH7Kf/43/

function phase_switch(){
    var phase;
    clicker = document.getElementById("click");
    phase=1
    switch (phase) {
        case 1:
            clicker.onclick = do_this;
            break;
        case 2:
            clicker.onclick = do_that;
            break;
    }

    function do_this (){
        alert("this");
        phase==2;
    }
    function do_that (){
        alert("that");
        phase==1;
    }
}

window.addEventListener("load", phase_switch());

As you can probably tell Phase one is the only phase that's firing. Why isn't phase 2 firing?


Solution

  • You can do it more simple:

    var clicker = document.getElementById("click");
    clicker.onclick = do_this;
    function do_this (){
        alert("this");
        clicker.onclick = do_that;
    }
    function do_that (){
        alert("that");
        clicker.onclick = do_this;
    }
    

    http://jsfiddle.net/Ncz7w/