2012-12-27 45 views
-4

问题:detectLocationtimersset()逻辑似乎不工作。我知道子例程被解雇,但我猜如果其他人错了?HTML5 Javascript嵌套如果其他工作没有按预期工作

detectLocationtimersset()的原因是有一些逻辑来确保没有设置多个定时器。

我创建了第一个if/else来根据datatime设置定时器,然后我写了第二个if/else来进行检测。

detectLocation()已经过测试并在其自身的工作。

var detectLocationtimerset = 0; 
var datatime = 1; 

function detectLocationtimersset() {  
    // Setup timer to check for new data start 
    // check and see if set 
    if (detectLocationtimerset = 0) { 
     detectLocationtimerset = 1; 
     if (datatime == null) { 
      datatime = 9; 
      milliseconds = (datatime * 60000) 
      setInterval(function() { 
       detectLocation(); 
       console.log("detectLocation() timer set"); 
      }, milliseconds); 
     } else { 
        detectLocationtimerset = 1; 
      milliseconds = (datatime * 60000) 
      setInterval(function() { 
       detectLocation(); 
       console.log("detectLocation() timer set"); 
      }, milliseconds); 
     } 
    } 
}; 
+1

这是什么,你不指望? –

回答

4

我不知道是什么问题,但

if (detectLocationtimerset = 0) 

也许应该

if (detectLocationtimerset === 0) 

在其他注意事项,

  • 您缩进应该是一致的
  • 各地运营商的间距应该是一致的
  • 你应该更喜欢identity在平等尽可能
  • 你应该将共享代码出的if/else块 - 把milliseconds分配和setInterval通话if (datatime == null)
+0

好抓!我忽略了两次:) –

+0

问:=&==&===做什么不同?我认为==是一个完全匹配,所以不是100%确定===。 T –

+0

@TerranBrown ===完全匹配。 –

2

if (detectLocationtimerset = 0) { 

应该

if (detectLocationtimerset === 0) {