2012-05-05 57 views
0

我有一个getJSON函数,我目前调用一次,然后用setInterval()函数来侦听可能会更改任何值的thingspeak.com通道源时间。从getJSON调用返回的数据更改触发器事件

当我返回的数据field1的值为'1'时,我想在我的jQuery代码中,在getJSON函数之外触发一个事件。如果该值为'0',它应该关闭事件。到现在为止还挺好。但是由于getJSON正在监听信道馈送几秒钟,它会一遍又一遍地触发事件(timer()函数)。

我该如何让getJSON事件“在后台运行”,并且只有在通道提要返回的数据实际发生更改时才会触发?返回的数据还有一个字段,其中包含数据条目的唯一标识(entry_id),因此可以监听此值的更改。

目前运行此代码:

$(document).ready(function() { 
    getUpdates(); 
    setInterval('getUpdates()',400); 
}); 

function getUpdates() { 
    $.getJSON('http://api.thingspeak.com/channels/xxx/feed/last.json?callback=?', {key: "xxx"}, function(data) { 
    if(data.field1 == '1') { 
     // trigger timer function on 
    } else if(data.field1 == '0') { 
     // trigger timer function off 
    } 
}); 

function timer() { 
    // Starts a timer countdown in a div 
}    

下面的代码可能更为翔实的第二个版本:

$(document).ready(function() { 
    getUpdates(); 
    setInterval('getUpdates()',400);  
}); 

function getUpdates() { 
    var entries = new Array(); 
    $.getJSON('http://api.thingspeak.com/channels/xxx/feed/last.json?callback=?', {key: "xxx"}, function(data) { 
     if ($.inArray(data.entry_id,entries) === -1) { 
      //New entry, add the ID to the entries array 
      entries.push(data.entry_id); 

      //Check if the div should be visible or not 
      if(data.field1 == '1') { 
       $(".desc").show(); 
      } else if(data.field1 == '0') { 
       $(".desc").hide(); 
      } 
     } else if ($.inArray(data.entry_id,entries) > -1) { 
      // Same entry as previous call, do nothing. 
     } 
    }); 
} 

<div class="desc"></div> 

这还是因为它似乎它不更新不工作条目数组。

+0

你的逻辑对我没有意义。如果数据字段为1 - 调用'timer()',如果数据为0 - 调用'timer()'。你什么时候不**不想触发'timer()'函数? – montrealist

+0

完全同意dalbaeb。你的逻辑对我来说也没有任何意义。每隔400毫秒检查一次json对象是否发生了变化。如果它确实改变了你不想触发一个计时器,如果它没有改变,你想停止计时器。这意味着您的计时器只能运行400毫秒,因为getUpdates()间隔会每400毫秒检查一次更改。你能解释一下这个计时器将要做什么吗? – Wezelkrozum

+0

感谢您的反馈!你直接指向我的问题@Wezelkrozum,可能是我正在讨论这个错误。如果data.field1等于1,我想启动一个应该运行一分钟的计时器。如果在那段时间field1的值已经改变为0,我想停止定时器。现在的问题是每400毫秒一个新的定时器的代码集,当我只希望第一次将field1设置为1时,然后再次将field1设置为0,然后再次设置为1之后,它进行这种设置。那有意义吗? – user1376918

回答

0

这是一个近似的,未经测试的逻辑(毕竟我无法使用正确的数据访问Feed)。让我知道它是否引导你朝着正确的方向前进。

var timerSet = 0; 

function startTimer(){ 
    timerSet = 1; 
    // start the 1-minute timer and show feedback to user 
} 

function stopTimer(){ 
    timerSet = 0; 
    // stop the 1-minute timer and show feedback to user 
} 

function getUpdates() { 
    var entries = new Array(); 
    $.getJSON('http://api.thingspeak.com/channels/xxx/feed/last.json?callback=?', {key: "xxx"}, function(data) { 
     if ($.inArray(data.entry_id,entries) === -1) { 
      //New entry, add the ID to the entries array 
      entries.push(data.entry_id); 

      // check which state the system is in and manage the 1-minute timer 
      if(data.field1 == '1' && timerSet === 0) { // if 1-minute timer not running already - start it 
       startTimer(); 
      } else if(data.field1 == '0' && timerSet === 1) { // if 1-minute timer running already - stop it 
       stopTimer(); 
      } 
     } 
    }); 
} 

$(document).ready(function() { 
    // getUpdates(); don't need this: function will be called in 400ms anyway 
    setInterval('getUpdates()',400);  
});