私は今、クロム拡張機能を構築しています。
毎日 23:59:59 にアラームが鳴るように設定したいので、1 日の終わりに保存した設定をリセットできます。
以下にこのコードを書きましたが、Date オブジェクトの使用に慣れていません。そして、このコードがうまく機能し、すべてを台無しにしないか心配です。
特に翌日のアラーム再設定のコードが気になります。
var day = now.getDate() + 1;
翌日の日付を取得するには、「now.getDate()」の戻り値に 1 を足すだけです。ただ、「now.getDate()」の戻り値が月末なのか、1を足すと存在しない日付になってしまう。
私のコードを見て、これがうまくいくかどうか教えてください。
前もって感謝します!!
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
var day = now.getDate();
var timestamp = Number(new Date(year, month, day, 23, 59, 59, 0));
//set an alarm for today at 23:59:59.
chrome.alarms.create('resetSpentTime', {
when: timestamp
});
// when alarm fires, do the following.
chrome.alarms.onAlarm.addListener(function() {
//clear some saved settings at the end of day.
//After that, set an alarm again for tomorrow at 23:59:59.
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
var day = now.getDate() + 1;
var timestamp = Number(new Date(year, month, day, 23, 59, 59, 0));
//set an alarm for tomorrow at 23:59:59.
chrome.alarms.create('resetSpentTime', {
when: timestamp
});
})