vue.js를 사용하여 타이머를 만들려고했지만 몇 가지 문제가 있습니다.두 가지 문제점 : this.myMethod가 addEventListner에 의해 추가 된 function-click 이벤트가 아니기 때문에 사용자가 클릭 할 때 더 많은 횟수가 발생합니다.
다음methods: {
saveRunningMethod() {
var runningData = {
duration: `${this.hour} : ${this.minute} : ${this.second}`,
username: this.$store.state.user.username
}
this.$store.dispatch('saveRunning' , runningData)
console.log(runningData);
},
startTimer(){
this.isTimerStart = true;
var timer = window.setInterval(() => {
var e = document.getElementById("stopBtn")
e.addEventListener("click", function(){
clearInterval(timer)
this.isTimerStart = false;
console.log("lets Save it")
this.saveRunningMethod()
});
if(this.mSecond < 9)
this.mSecond +=1
else
this.mSecond=0
if(this.mSecond==9)
this.second +=1
if(this.second>59)
this.second=0
if(this.second==59)
this.minute +=1
if(this.minute>59)
this.minute = 0
if(this.minute==59)
this.hour +=1
},100);
}
}
, 내 e.addEventListener("click", function(){
내 정지 버튼을 클릭합니다,하지만 당신은이 사진에서 볼 수있는대로,이 방법은 여러 번 실행 보인다 때 타이머를 중지, 내 CONSOLE.LOG이 내 mtehods 섹션입니다 33 번 같이 달린다! 그게 왜? 내 startTimer()
방법 내에서이 방법을 실행 this.saveRunningMethod()
하지만 "this.saveRunningMethod() is not a function"
오류 :
나의 다른 질문은,이 라인에 관한 것입니다!
마지막으로 더 나은 솔루션을 알고 있다면 setInterval
을 사용하여 타이머를 만들었습니다. 정말 감사하겠습니다.
업데이트 : 나는
<div class="row p-2 m-3 mt-3">
<div class="col-12 p-0 animated fadeInUp mt-3">
<p class="text-center">Your last record was : 00:00:00</p>
</div>
<div class="col-12 p-0 animated fadeInUp mt-3">
<h1 class="text-center timer">
{{this.hour}}:{{this.minute}}:{{second}}:{{mSecond}}
</h1>
</div>
</div>
<div class="row p-2 mt-3" v-bind:class="[this.isTimerStart==false ? 'show' : 'hide']">
<div class="col-12 p-0 animated tada text-center">
<button class="btn-link timerImg" @click="startTimer()">
<img class="img-fluid timerImg" src="../../static/timerStart.png" />
<p>Start</p>
</button>
</div>
</div>
<div class="row p-2 mt-3" v-bind:class="[this.isTimerStart ? 'show' : 'hide']">
<div class="col-12 p-0 animated tada text-center">
<button id="stopBtn" class="btn-link timerImg">
<img class="img-fluid timerImg" src="../../static/timerStop.png" />
<p>Stop</p>
</button>
</div>
</div>
감사합니다 내 HTML 부분에 추가합니다.
가 명확하지 않다,하여 여러개의 시간을 실행하는 타이머가 이유. 질문을 편집하고 HTML 부분도 추가하십시오. – WaldemarIce
@WaldemarIce 내 HTML 부분은 타이머와 버튼 두 개를 표시하는 div 중 하나입니다. 질문을 업데이트하고 어쨌든 추가합니다. 감사. –
한 번에 한 가지 질문 만하십시오. https://stackoverflow.com/help/how-to-ask – Rob