语言

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
编辑者为: houssain elbou 11 8月, 2018

响应

4 回复
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.

houssain elbou
If I'm understand it good I have to pause setinterval to make watchposition to pause? Instead of using getTime() is it not beter to use like setInterval(isRunning,1000) when its true then start the watch? I still dont understand the getTime() what does it do?
houssain elbou
If I'm understand it good I have to pause setinterval to make watchposition to pause? Instead of using getTime() is it not beter to use like setInterval(isRunning,1000) when its true then start the watch? I still dont understand the getTime() what does it do?