--- title: ServiceWorker.onstatechange slug: Web/API/ServiceWorker/onstatechange translation_of: Web/API/ServiceWorker/onstatechange ---
Обработчик события, вызываемый при срабатывании события statechange
; по сути, срабатывает при изменении {{domxref("ServiceWorker.state")}}.
ServiceWorker.onstatechange = function(statechangeevent) { ... } ServiceWorker.addEventListener('statechange', function(statechangeevent) { ... } )
Данный фрагмент кода из примера событий service worker (демо) возвращает состояние при каждом его изменении.
var serviceWorker; if (registration.installing) { serviceWorker = registration.installing; document.querySelector('#kind').textContent = 'installing'; } else if (registration.waiting) { serviceWorker = registration.waiting; document.querySelector('#kind').textContent = 'waiting'; } else if (registration.active) { serviceWorker = registration.active; document.querySelector('#kind').textContent = 'active'; } if (serviceWorker) { logState(serviceWorker.state); serviceWorker.addEventListener('statechange', function(e) { logState(e.target.state); }); }
Обратите внимание, что при срабатывании statechange
, ссылки на service worker могли измениться. Например:
navigator.serviceWorker.register(..).then(function(swr) { swr.installing.state == "installing" swr.installing.onstatechange = function() { swr.installing == null; // At this point, swr.waiting OR swr.active might be true. This is because the statechange // event gets queued, meanwhile the underlying worker may have gone into the waiting // state and will be immediately activated if possible. } })
Спецификация | Статус | Комментарии |
---|---|---|
{{SpecName('Service Workers', '#dom-serviceworker-onstatechange', 'ServiceWorker.onstatechange')}} | {{Spec2('Service Workers')}} | Изначальное определение |
{{Compat("api.ServiceWorker.onstatechange")}}