2013-02-13 3 views
0

나는 특정 목표 정확성이 획득 될 때까지 장치 위치를 얻는 phonegap 앱을 개발중인 javascript 스크립트를 가지고있다. 대상이 충족되지 않으면 스크립트는 다시 자신을 영원 토록 호출합니다. 일반적으로 위치 및 사용 된 마지막 시간에 따라 1-8 회 시도가 소요되며 모든 것이 정상적으로 작동합니다. 내가 겪고있는 문제는 일부 장치가이 임계 값에 도달하지 못하는 것 (앱에서는 괜찮음)이지만이 스크립트는 백그라운드에서 계속 실행되기 때문에 배터리 수명이 파괴된다는 것입니다. 그렇다면 클릭 이벤트 (제출 버튼)를 사용하여이 스크립트를 어떻게 죽일 수 있습니까? 제출 버튼이 바뀌는 글로벌 var을 설정하려고 생각했습니다. 예 :클릭 루프에서 자바 스크립트 종료

if (var != 'quit') { 
    //function 
} else { 
    ///nothing 
} 

function getlocation(position) { 
    document.getElementById("lat").value = (position.coords.latitude); 
    document.getElementById("long").value = (position.coords.longitude); 
    document.getElementById("accu").value = (position.coords.accuracy); 
    var accuracy = parseInt(position.coords.accuracy, 10); 
    if (accuracy <= 50) { 
     $('#scanner').removeClass('littlered').addClass('littlegreen'); 
    } else { 
     setTimeout(navigator.geolocation.getCurrentPosition(getDroplocation, onError, { 
      enableHighAccuracy: true 
     }), 3000); 
    } 
} 

이 기능은 작동하지 않습니다. 전역 변수를 설정했지만 아무 일도 일어나지 않습니다. 앞서 말한 아이디어를 얻는 더 좋은 방법이나 방법은 없을까요? 나는 올바른 길을 가고 있는가?

답변

0

clearTimeout() 사용 방법을 사용 TER 예

<input type="button" id="togglebutton" value="Stop" /> 

이처럼

10 개 시도 이후에는 죽의 setTimeout() 메소드를 설정하는 타이머를 클리어한다.

var to; 

function getlocation(position) { 
    document.getElementById("lat").value = (position.coords.latitude); 
    document.getElementById("long").value = (position.coords.longitude); 
    document.getElementById("accu").value = (position.coords.accuracy); 
    var accuracy = parseInt(position.coords.accuracy, 10); 
    if (accuracy <= 50) { 
     $('#scanner').removeClass('littlered').addClass('littlegreen'); 
    } else { 
     to = setTimeout(navigator.geolocation.getCurrentPosition(getDroplocation, onError, { 
      enableHighAccuracy: true 
     }), 3000); 
    } 
} 

//Add click event handler to a button to stop the looping 
var button = document.getElementById("testbtn"); 
testbtn.onclick = function() { 
    clearTimeout(to); 
} 
+0

이후로 내 iPhone에서 더 많은 코드를 쉽게 게시 할 수 없다는 것이 나쁘다. 어떻게 나중에 다시 추가 할 수 있습니까? 몇 분 후에 다시 사용할 수 있도록이 스크립트가 필요합니다. – centree

+0

다시 – frosdqy

+0

setTimeout() 메서드를 실행할 함수를 호출하고 =로 설정하면? – centree

0

당신은 클릭하거나 coun에 추가 할 clearTimeout

변경

function getlocation(position) { 

setTimeout

tId=setTimeout에 그리고 당신이 원하는대로의 clearTimeout(tId); 온 클릭을

var tId; 
function getlocation(position) { 

에하고 싶은

var tId,startPosition="whateverdefaultpositionissetto"; 
window.onload=function() { 
    document.getElementById("togglebutton").onclick = function() { 
    if (tId) { 
     clearTimeout(tId); 
     this.value="Start"; 
    } 
    else { 
     getlocation(startPosition); 
     this.value="Stop"; 
    } 
    } 
} 
+0

어떻게 나중에 다시 추가 할 수 있습니까? 몇 분 후에 다시 사용할 수 있도록이 스크립트가 필요합니다. 클릭하면 클릭하면 될까요? – centree

+0

아무 것도. 허용 된 답변이 동일하지만 나중에 내 – mplungjan