How to create button for traffic light code in javascript?

前端 未结 1 577
孤城傲影
孤城傲影 2021-01-29 11:32

I intend to include a button in my traffic light code so that the user can click on it to activate the traffic light however, i am unsure of where to place it in my code.

相关标签:
1条回答
  • 2021-01-29 12:34

    Place the button wherever you like inside the body tag.

    <input type="button" name="start" value="Start/stop Traffic light" onclick="startStop()">
    

    Remove the setTimeout and the slideit() initial call.

    var step=1;
    function slideit()
    {
        document.images.slide.src = eval("image"+step+".src");
        if(step<4)
            step++;
        else
            step=1;
    }
    

    And add the startStop function below and the working variable declaration.

    var working;
    function startStop() {
        if (typeof working != 'undefined') working = clearInterval(working);
        else working=setInterval("slideit()", 3000);
    }
    
    0 讨论(0)
提交回复
热议问题