2014-02-07 4 views
0

그래서이 솔루션이 효과가있을 것이라고 생각하지만 좀 더 우아한 것이 있는지보고 싶었습니다. 따라서 사용자가 위치에 "체크인"할 수있게하고 싶다면 내가 그 위치에 남아 있는지 확인하고 싶습니다. 나는 약 5 분마다 점검 할 것이다. 일단 그들이 체크 아웃하면 나는 검사를 멈출 수 있습니다. 목표는 상점에서 소비 한 총 시간을 얻는 것입니다. Parse 용 Javascript SDK를 사용하고 있습니다. 이것은 내 솔루션의 기본 요령이 아닌 완벽한 솔루션입니다. while 루프를 사용하여 사용자 위치를 확인하고 체크인 한 채로 계속 수행하십시오. 제안 및 도움에 감사드립니다. (또한 Google지도 API를 사용하여 상점 위치를 얻습니다.) 내 코드가 맞으면 내 접근 방식이 올바른지 더 자세히 묻는 것입니다.매 5 분마다 사용자 위치 확인 javascript

checkIn: function(){ 

store_location = "something predefined"; 
Parse.GeoPoint.current({ 
    success: function(point){ 

     var user_location = new google.maps.LatLng(point.latitude,point.longitude); 
     if(user_location = store_location){ 
      this.checkedIn(); 
     }, 
    } 

}); 
}, 
checkedIn: function(){ 
///run loop while the user is still checked in 
///increment total time by 5 min every time loop is called 

var checked_in = true; 
total_time = 0; 

while(checked_in){ 
    setTimeout(function(){ 
     var user_location = new  google.maps.LatLng(point.latitude,point.longitude); 
     if(user_location = store_location){ 
      total_time=total_time + 5 
     }else{ 
      checked_in = false; 
     } 
    }, 3000000) 
} 

} 대신에 잠시와의 setTimeout을 사용

+1

while 루프는 전혀 작동하지 않는다. while 루프에서 돌아가서 시스템에 자원이 다 소모 될 때까지'setTimeout()'타이머를 만듭니다. 시간 제한이 실행되지 않습니다. 이것은 단순히 자바 스크립트로 프로그래밍 할 수있는 방법이 아닙니다. 타이머를 설정해야하며 타이머가 작동하면 계속해서 다른 타이머를 설정할지 여부를 결정합니다. – jfriend00

답변

0
window.setInterval(function(){ 
     var user_location = new  google.maps.LatLng(point.latitude,point.longitude); 
     if(user_location = store_location){ 
      total_time=total_time + 5 
     }else{ 
      checked_in = false; 
     } 
    },3000000); 
+0

아, 그게 훨씬 낫습니다. 감사! 난 자바 스크립트 멍청한 녀석이다 – David

+1

'setInterval' 만 사용하고'setTimeout'을 제거해야합니까? – Phrogz

+0

나는 당신의 권리를 믿는다. 그러나 그는 내가 그것을 어떻게 썼는지에 대해서 혼란 스러웠을지도 모른다. setInterval (function() {doSomething()}, 3000); 올바른 구현은 – David

0

, 왜 setInterval을을 사용 해달라고? 나는 그것이 더 쉽고 코드를 단순화한다고 생각한다.

+0

네가 맞아. 나는 setInterval에 대해서조차 몰랐다. 자바 스크립트를 처음 사용하는 경우 – David

관련 문제