LoginSignup
14
14

More than 5 years have passed since last update.

iOS7でPage Visibility APIを使う

Last updated at Posted at 2013-10-01

iOS7のSafariからPage Visibility APIが使える様になったとのことなので試してみました。

var count = 0,
    increase = false,
    countEl = document.getElementById('count');

setInterval(function() {
    increase && (countEl.innerText = ++count);
}, 100);

document.addEventListener('visibilitychange', function() {
    increase = document.hidden;
});

実機での確認はこちらから

Chromeではプレフィックスが付いてwebkitvisibilitychange,
document.webkitHiddenでしたが、iOS7のsafariでは不要のようです。

実機で動かしてみると右下のボタンを押してタブ選択中の状態でのみ
countが加算されます。

シングルページのWebアプリなどで画面を離れて一定時間以上経ったら
戻ってきたときにデータを更新するとか、色々と使い道はありそうな予感です。

14
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14