簡易ストップウォッチを作ってみる[Aura]
概要

Auraで簡易ストップウォッチを作成します。
setTimeoutで1000ミリ秒毎に秒数をカウントアップします。
ソースコード
SimpleStopwatchAura.cmp
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="sec" type="Integer" default="0"/>
<aura:attribute name="min" type="Integer" default="0"/>
<aura:attribute name="timerId" type="String"/>
<aura:attribute name="disableStartButton" type="Boolean" default="false"/>
<aura:attribute name="disableStopButton" type="Boolean" default="true"/>
<lightning:card>
<div class="slds-grid slds-einstein-header slds-card__header">
<header class="slds-media slds-media_center slds-has-flexi-truncate">
<div class="slds-grid slds-grid_vertical-align-center slds-size_3-of-4 slds-medium-size_2-of-3">
<h1 class="slds-truncate" title="簡易ストップウォッチAura">
<span class="slds-text-heading_large">簡易ストップウォッチAura</span>
</h1>
</div>
<div class="slds-einstein-header__figure slds-size_1-of-4 slds-medium-size_1-of-3"></div>
</header>
</div>
<div class="slds-align_absolute-center">
<div class="slds-m-top_large">
<header class="slds-col slds-size_1-of-1 slds-align_absolute-center" >
<div aura:id="curentTime" class="slds-text-heading_large">00:00</div>
</header>
<div class="slds-align_absolute-center">
<div class="slds-button-group slds-m-vertical_medium" role="group">
<lightning:button variant="neutral" label="スタート" onclick="{!c.startApp}" class="slds-text-heading_medium slds-p-around_small" disabled="{!v.disableStartButton}"></lightning:button>
<lightning:button variant="neutral" label="ストップ" onclick="{!c.startApp}" class="slds-text-heading_medium slds-p-around_small" disabled="{!v.disableStopButton}"></lightning:button>
<lightning:button variant="neutral" label="リセット" onclick="{!c.startApp}" class="slds-button_text-destructive slds-text-heading_medium slds-p-around_small"></lightning:button>
</div>
</div>
</div>
</div>
</lightning:card>
</aura:component>
SimpleStopwatchAuraController.js
({
// 押されたボタンを判定する
startApp : function(component, event, helper) {
const clickedButtonlabel = event.getSource().get("v.label");
if(clickedButtonlabel === 'スタート') {
// 計測開始
helper.startTimmer(component);
} else if(clickedButtonlabel === 'ストップ') {
// 計測終了
helper.stopTimmer(component);
} else {
// 計測リセット
helper.resetTimmer(component);
}
}
})
SimpleStopwatchAuraHelper.js
({
// 計測開始
startTimmer : function(component, event) {
component.set("v.disableStartButton", true);
component.set("v.disableStopButton", false);
// 1秒毎に計測する
this.countUp(component);
},
// 計測終了
stopTimmer : function(component, event) {
component.set("v.disableStartButton", false);
component.set("v.disableStopButton", true);
clearTimeout(component.get("v.timerId"));
},
// 計測リセット
resetTimmer : function(component) {
component.set("v.disableStartButton", false);
component.set("v.disableStopButton", true);
clearTimeout(component.get("v.timerId"));
const curentTimeText = component.find('curentTime').getElement();
curentTimeText.innerHTML = "00:00";
component.set("v.min", 0);
component.set("v.sec", 0);
},
// 計測時間を分に変換する
convertTime : function(component) {
let min = component.get("v.min");
let sec = component.get("v.sec");
sec++;
if (sec >= 60) {
sec = 0;
min++;
}
component.set("v.min", min);
component.set("v.sec", sec);
},
// 1秒毎に計測する
countUp : function(component) {
const timerId = setTimeout(function(){
// 計測時間を分に変換する
this.convertTime(component);
let min = component.get("v.min");
let sec = component.get("v.sec");
// 桁数を固定
min = ('0' + min).slice(-2);
sec = ('0' + sec).slice(-2);
// 計測時間を表示
const curentTimeText = component.find('curentTime').getElement();
curentTimeText.innerHTML = min + ":" + sec;
// 1秒毎に計測する
this.countUp(component);
}.bind(this),1000);
component.set("v.timerId", timerId);
}
})

