Languages

Menu
Sites
Language
How to pause/restart watchpostion
Hi, I know how you can stop watchposition in geolocation by using watchid and clearwatch. But is it also possible to pause/restart the watch? If yes can someone provide me an example for I can do that. Thank you
Edited by: houssain elbou on 11 Aug, 2018

Responses

2 Replies
Marco Buettner
var watchTimeId = 0,
    isRunning = false;

function getTime() {
    //TODO: TIME FUNCTION
}

function stopWatch() {
    clearInterval(watchTimeId);
}

function startWatch() {
    if(!isRunning) {
        getTime();
        watchTimeId = setInterval(getTime, 1000);
    }
}

 

Marco Buettner

Some explanations to the code.

Watches are typical recalled by setInterval and return like setTimeout an Identify (IntervalId). The counter-function of them is clearInterval and get the IntervalId as parameter.  It stops the running-cycle of the intervals. To restart you need to recall the setInterval.

startWatch should be load on boot/start/domcontentloaded/init/restart whatever.