将用户输入存储为var

时间:2016-07-19 23:52:24

标签: javascript

所以这是一个计算每小时订单的计时器。当我硬编码订单时,一切都按我想要的方式工作。但我似乎无法想象如何获得用户输入来设置订单量。这是我一直在尝试的,它不起作用。任何想法??

<html>
<body>

    <p><strong>Time Elapsed:</strong><span id="time-elapsed"</span></p>

    <button onclick="openFunc()">Start Time</button>

    <button onclick="myStopFunction()">Stop time</button><br><br>

    <input type="text" id="orderAmount"><br>Enter Number of Orders</input><br><br>

    <button onclick="calculate(timeElapsed, orders)">Calculate...</button>


    <script>
        var timeElapsed = 0;
        var myVar = '';
        var average = 0;
        var orders = document.getElementById("orderAmount").innerHTML += orders;


        function openFunc(){
            start();

        }

        function calculate(time, _orders) {
            var avg = _orders/(time/_orders);
            if (avg<1){
                avg = 1;
            }
            document.write(avg + " Bags per Hour");
        }   

        function start() {
            if ( myVar != "" ) { 
                console.log('timer is running');
                return; 
            }
            clearInterval(myVar);
            myVar = setInterval(function(){ 
                    myTimer() 
            }, 1000);
        }


            function myTimer() {
                timeElapsed += 1;
                document.getElementById("time-elapsed").innerHTML =    formatTime(timeElapsed);
        }

            function formatTime(time) {
            var hours = formatNumber(Math.floor(time/3600))
            var minutes = formatNumber(Math.floor(time/60));    
            var seconds = formatNumber(time%60);    

            //console.log('minutes: ', minutes);
            //console.log('seconds: ', seconds);
            return hours + ":" + minutes + ":" + seconds
        }

            function formatNumber(number) {
            if ( number < 10 ) {
                return 0 + '' + number  
            }   
            return number
        }

            function myStopFunction() {
                clearInterval(myVar);
                myVar = "";
        }
        </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

这是您使用orderAmount id:

从文本字段获取用户输入的方式
var orders = document.getElementById("orderAmount").value;