2017-05-28 103 views
0

我有一個計時器,每秒向變量MenuTimer加1。 我希望在接下來的按鈕被按下TillOpen是該MenuTimer將停止爲1添加到它之後和一個新的變量有1個附加,而不是PackTime增量問題

window.onload = function() { 
    var StopwatchSeconds= 00; 
    var StopwatchMinutes = 00; 
    var ShowSeconds = document.getElementById("seconds"); 
    var ShowMinutes = document.getElementById("minutes"); 
    var StartButton = document.getElementById("ButtonStart"); 
    var Interval; 
    var menuTime; 
    var serviceTime; 
    var orders; 
    var menuAvg; 
    var serviceAvg; 

    StartButton.onclick= function(){ 
     clearInterval(Interval); 
     Interval = setInterval(startTimer, 1000); 
    } 

    function startTimer() { 
     StopwatchSeconds++; 

     if(StopwatchSeconds > 59) { 

      ShowSeconds.innerHTML = "0" + StopwatchSeconds; 
      StopwatchSeconds = 0; 
      ShowMinutes.innerHTML = StopwatchMinutes; 
      StopwatchMinutes++; 
     } 

     if(StopwatchSeconds < 59) { 
      ShowSeconds.innerHTML = StopwatchSeconds; 
     } 
    } 
} 
+1

什麼是你的代碼做的,有什麼問題呢? – 4castle

+0

計劃是爲代碼計時「菜單」和「直到打開」需要多長時間。一旦我有了這個時間,也能夠顯示兩者的平均數以及兩者的平均數。 例如。菜單有10秒,40秒和20秒。直到打開有5秒,10秒和30秒。所以我們可以得到Menu的平均值和Till平均值,但也可以將兩者的平均值放在一起。例如10 + 5s,40 + 10s和20 + 30s。 – user3500310

回答

0

新的解決方案,至極允許創建不同的定時器和跟蹤他們:

//a method to setup a new timer 
function Timer(Name){ 
    this.timeElement=document.createElement("div"); 
    (this.stopButton=document.createElement("button")).innerHTML="STOP"; 
    (this.startButton=document.createElement("button")).innerHTML="START"; 
    (this.Name=document.createElement("h1")).innerHTML=Name; 
    [this.Name,this.timeElement,this.startButton,this.stopButton].forEach(el=>document.body.appendChild(el)); 

    this.stopButton.addEventListener("click",this.stop.bind(this)); 
    this.startButton.addEventListener("click",this.start.bind(this)); 

    this.seconds=0; 
    this.minutes=0; 
} 

Timer.prototype={ 

    update:function() { 
     this.seconds++; 
     if(this.seconds > 59) { 
      this.seconds=0; 
      this.minutes++; 
     } 
     var secTemp="00"+this.seconds, minTemp="00"+this.minutes; 
     this.timeElement.innerHTML=minTemp.slice(minTemp.length-2)+":"+secTemp.slice(secTemp.length-2); 
    },  


    stop:function(){ 
     if(this.interval) clearInterval(this.interval); 
     this.running=false; 
     if(this.onstop) this.onstop(this); 
    } 

    start:function(){ 
     if(this.interval) clearInterval(this.interval); 
     this.interval = setInterval(this.update.bind(this), 1000); 
     this.running=true; 
     if(this.onstart) this.onstart(this);    
    } 
}; 

這實現了與OOP的計時器。所以你可以創建多個定時器,它們不會互相影響。

您可以創建這樣一個計時器:

var timer= new Timer("The Name"); 

你也可以改變事件,設置/讀取的時間,並檢查是否正在運行:如果你想等待多個定時器

timer.start();//start the timer (can also be done with the ui button) 
timer.stop(); 
timer.onstart=()=>alert("Started!"); 
timer.onstop=()=>alert("Stopped!"); 
console.log(timer.running,timer.minutes,timer.seconds); 

並計算平均值,如果所有的人都停止了:

var timers=["Timer 1", "Timer 2"].map(name=>new Timer(name));//create two timers and store in array 
timers.forEach(function(timer){ 
    timer.running=true; 
    timer.onstop=function(){ 
     if(timers.some(t=>t.running)) return;//if theres a running timer dont procceed 

     var seconds=timers.reduce((seconds,timer)=>seconds+=(timer.seconds+timer.minutes*60),0); 
     var average=seconds/timers.length; 
     alert("Average: "+average+"s"); 
    }; 
});  

http://jsbin.com/coduvohewu/edit?output

這個老辦法,增加一個計時器,如果新的按鈕被按下,並停止舊的然後:

所以,你要停止當前的定時器,下面創建一個新的?也許你可以重構代碼了一下,做某事是這樣的:

window.onload = function() { 
var seconds= 0,minutes = 0; 
var times=[]; 
var Interval; 
var timeElement; 

//a method to setup a new timer 
function createTimer(dontsave){ 
    if(times.length>3) return alert(times.map(el=>el.join(":")).join()); 
    timeElement=document.createElement("div"); 
    document.body.appendChild(timeElement); 
    if(!dontsave) times.push([minutes,seconds]); 
} 

createTimer(true); 

//a method to let the timer run 
function startTimer() { 
    seconds++; 
    if(seconds > 59) { 
     seconds=0; 
     minutes++; 
    } 
    var secTemp="00"+seconds,minTemp="00"+minutes; 
    timeElement.innerHTML=minTemp.slice(minTemp.length-2)+":"+secTemp.slice(secTemp.length-2); 
}  

//assign to buttons: 

document.getElementById("ButtonStart").onclick= function(){ 
clearInterval(Interval); 
Interval = setInterval(startTimer, 1000); 
} 

document.getElementById("ButtonNew").onclick=createTimer; 

}; 

http://jsbin.com/mujisaweyo/edit?output

如果你按下一個按鈕,與ID ButtonNew這只是創建DOM中的新的div。所以當前時間保持爲舊元素中的文本,並且在新元素中保持計數。我也添加了一個零填充...

+0

哦,那真的很棒。問題是3個定時器xD或至少3個變量來保存定時器,如果這是有道理的話。所以我想讓一個Timer定時「菜單」,另一個定時「打開」。但在此之後,我需要得到兩者的平均值。以及平均總時間。 – user3500310

+0

@ user3500310 ive添加了幾行以將值存儲在數組中。不過,我認爲你的任務需要一些不同的方法。我會努力... –

+0

這真的很有幫助。非常感謝!我也在繼續努力,看看我能否得到它的工作。或者我應該嘗試尋找新的方法? – user3500310

0

這就是它的全部。它有一半有效,但希望你能更好地理解我正在努力去做什麼。

var Interval; 
var PackInterval; 
var StopwatchSeconds= 00; 
var StopwatchMinutes = 00; 
var ShowSeconds = document.getElementById("seconds"); 
var ShowMinutes = document.getElementById("minutes"); 
var StartButton = document.getElementById("ButtonStart"); 
var TillOpenButton = document.getElementById("TillOpen"); 
var FinishButton = document.getElementById("Finish"); 
var ShowMenuTime = document.getElementById("MenuTime"); 
var ShowPackTime = document.getElementById("PackTime"); 
var ShowPackAvgSeconds = document.getElementById("PackerSeconds"); 
var ShowPackAvgMinutes = document.getElementById("PackerMinutes"); 
var ShowMenuAvgSeconds = document.getElementById("MenuMinutes"); 
var ShowMenuAvgMinutes = document.getElementById("MenuSeconds"); 
var DivisionSeconds = 60; 
var TotalTime = 0; 
var MenuTime = 0; 
var PackTime = 0; 
var AllMenuTimes = 0; 
var AllPackTimes = 0; 
var TotalMenuOrders = 0; 
var TotalPackOrders = 0; 
var MenuOrdersTotalSeconds = 0; 
var PackOrdersTotalSeconds = 0; 
var MenuAvgMinutes = 0; 
var MenuAvgSeconds = 0; 
var PackAvgSeconds = 0; 
var PackAvgMinutes = 0; 



StartButton.onclick = function(){ 
    TotalMenuOrders + 1; 
    MenuTime = 0; 
    ShowMenuTime.innerHTML = MenuTime; 
    clearInterval(Interval); 
    Interval = setInterval(startTimer, 1000); 

    window.alert ("I work"); 


} 

//This starts the timer. Inverval is a variable that holds the timer number. 

function startTimer() { 

    StopwatchSeconds++; 
    TotalTime++; 
    MenuTime++; 
    AllMenuTime++; 
    if(StopwatchSeconds > 59) { 

     ShowSeconds.innerHTML = "0" + StopwatchSeconds; 
     StopwatchSeconds = 0; 

      StopwatchMinutes++; 
     ShowMinutes.innerHTML = StopwatchMinutes; // Makes this a string in html 

    } 

    if(StopwatchSeconds < 59) { 
     ShowSeconds.innerHTML = StopwatchSeconds; 
    } 
} 

// When the start button is pressed this function starts. it adds 1 to 
Stopwatch, total and Menu every 1000 increments that Interval hits. 
// This also says if StopwatchSeconds goes above 59 itll reset to 0 and if 
its below itll keep counting. 

TillOpenButton.onclick = function() { 
    PackTime = 0; 
    ShowPackTime.innerHTML = PackTime; 
     ShowMenuTime.innerHTML = MenuTime; 
    PackInterval = setInterval(startPackerTimer, 1000); 
     Interval+PackInterval; 
     clearInterval(Interval); 


/* if (TotalMenuOrders < 1) { 

    AllMenuTimes/TotalMenuOrders = MenuOrdersTotalSeconds; 
MenuOrderTotalSeconds % 60 = MenuAvgSeconds; 
    MenuAvgMinutes = Math.floor(MenuOrderTotalSeconds/60); 

    ShowMenuAvgMinutes.innerHTML = MenuAvgMinutes; 
    ShowMenuAvgSeconds.innerHTML = MenuAvgSeconds; 
    } 
    */ 
    } 






// When this button is pressed it stops the first timer and the menu timer. 
It then starts a new timer and function which add to the variable that will 
show the total time. 
// It does clear the variable Interval though 

FinishButton.onclick = function(){ 
    clearInterval(Interval); 
    ShowPackTime.innerHTML = PackTime; 
    clearInterval(PackInterval); 
    StopwatchSeconds = 0; 
    StopwatchMinutes = 0; 
    ShowSeconds.innerHTMl = 0 + StopwatchSeconds; 
    ShowMinutes.innerHTML = 0 + StopwatchMinutes; 

    AllPackTimes += PackTime; 
    TotalPackOrders++; 

    /*AllPackTimes/TotalPackOrders = PackOrderTotalSeconds; 
    PackOrderTotalSeconds % DivisionSeconds = PackAvgSeconds; 

    PackAvgMinutes = Math.floor(PackOrderTotalSeconds/60); 

    ShowPackAvgMinutes.innerHTML = PackAvgMinutes; 
    ShowPackAvgSeconds.innerHTML = PackAvgSeconds;*/ 


} 

// When the Finish Button is pressed it clears everything. Resets 
everything. except Menu Time, Total Time and PackTime. I need 3 new 
variables to hold these to get the average. 






function startPackerTimer() { 

    StopwatchSeconds++; 
    TotalTime++; 
    PackTime++; 

    if(StopwatchSeconds > 59) { 

     ShowSeconds.innerHTML = "0" + StopwatchSeconds; 
     StopwatchSeconds = 0; 

     StopwatchMinutes++; 
     ShowMinutes.innerHTML = StopwatchMinutes; 

    } 

    if(StopwatchSeconds < 59) { 
     ShowSeconds.innerHTML = StopwatchSeconds; 
    } 
    // Same deal but with the Till open button. Still adds onto 
STopwatchSeconds so the variable doesn't change. 

}