ある一定の時間が経過したらJSの処理を行うように実装するにはどうすればいいのだろうか?
一般的には、setTimeoutやsetIntervalを使うんだけど、拡張機能の場合は、alarmsというAPIが用意されている。
//manifest.json
{
"manifest_version": 2,
"name":"hoge",
"version":"0.1",
"icons":{
"16":"images/icon_16.png",
"48":"images/icon_48.png",
"128":"images/icon_128.png"
},
"background": {
"scripts": ["eventPage.js"],
"persistent": false
},
"permissions": [
"alarms"
]
}manifest.jsonではパーミッションでalarmsを設定するのがミソ//アラームの登録
chrome.alarms.create('foo',{
'periodInMinutes':1
});
//アラームが発生した場合の処理
chrome.alarms.onAlarm.addListener(function(alarm){
});
delayInMinutesというプロパティもあるのですが、periodInMinutesだけ設定されている場合、periodInMinutes間隔で処理が発生します。定期処理をしたい場合に便利ですね!!
0 コメント:
コメントを投稿