2014-02-07 77 views
0

所以我认为这个解决方案可以工作,但只是想看看是否有更优雅的东西。所以我想让用户“登记”到一个位置,一旦他们这样做了,我想看看他们是否留在那个位置。我会每5分钟检查一次。一旦他们检查出来,然后我可以停止检查。目标是获得在商店中花费的总时间。我正在使用JavaScript SDK for Parse。这是我解决方案的基本问题,而不是一个完整的解决方案。使用while循环来检查用户的位置,并且只要他们保持登录状态,就可以继续这样做。感谢您的任何建议和帮助! (另外我正在使用Google Maps 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) 
} 

}

+1

while循环根本不工作。它只会在while循环中旋转,创建'setTimeout()'定时器,直到系统耗尽资源。永远不会有超时运行。这只是根本不是你可以用JavaScript编程的方式。您必须设置一个计时器,然后当该计时器触发时,您决定是否要继续并设置另一个计时器。 – 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

哦,你好得多。谢谢!我是一个java脚本noob – David

+1

你的意思是只使用'setInterval',并且需要删除'setTimeout'? – Phrogz

+0

我相信你的权利Phrogz,但他可能刚刚被我如何写它的困惑。的setInterval(函数(){doSomething的()},3000);是否正确执行 – David

0

而是采用了一会儿,setTimeout的,你为什么不使用的setInterval?我认为这是更容易和简化代码

+0

是的,你的权利。我只是不知道setInterval。新的javascript – David