obniz obnizボードに現在時刻表示するサンプルコード
■サンプルコード
<script>
obniz.onconnect = async function () {
//obnizボードに時間を表示
//デバイス接続中はループ
while (obniz.connectionState === 'connected') {
try {
//obnizボードの表示を初期化
obniz.display.clear();
//現在時刻を取得 ※('0' + 時刻)をslice(-2)してゼロパディング
var now = new Date();
var year = now.getFullYear();
var month = ('0' + (now.getMonth() + 1)).slice(-2);
var day = ('0' + now.getDate()).slice(-2);
var hour = ('0' + now.getHours()).slice(-2);
var minute = ('0' + now.getMinutes()).slice(-2);
var seconds = ('0' + now.getSeconds()).slice(-2);
obniz.display.print(year + '/' + month + '/' + day);
obniz.display.print(hour + ':' + minute + ':' + seconds);
//1秒待つ
await obniz.wait(1000);
} catch(e) {
//エラーの場合の処理
}
}
};
</script>