Javascript SetInterval仅使用Else一次

时间:2015-08-20 20:28:07

标签: javascript

我需要其他{}只能工作一次。我怎么能这样做?谢谢。

    setInterval(function(){
    mysqlConnection.query('SELECT `dog` FROM `pets` WHERE `name`=`nice`', function(err, row, fields) {
   var nice_pet = (row[0].dog);
                                                                mysqlConnection.query('SELECT `food` FROM `info`', function(err, row, fields) {
        mysqlConnection.query('SELECT COUNT(Distinct food) AS DistinctCount FROM `something', function(err, row, fields) {
            var check = row[0].DistinctCount;
            if (check <= 10) {
        // Do it many times
         } else {
        // Do it only once if it goes to here
         }
    })
    })
    })
    }, 1000);

1 个答案:

答案 0 :(得分:2)

制作一个标志,并将其用作else if

中的条件
var once = false;
setInterval(function(){
  mysqlConnection.query('SELECT `dog` FROM `pets` WHERE `name`=`nice`', function(err, row, fields) {
    var nice_pet = (row[0].dog);
    mysqlConnection.query('SELECT `food` FROM `info`', function(err, row, fields) {
      mysqlConnection.query('SELECT COUNT(Distinct food) AS DistinctCount FROM `something', function(err, row, fields) {
        var check = row[0].DistinctCount;
        if (check <= 10) {
          // Do it many times
        } else if(!once) {
          once = true;
          // Do it only once if it goes to here
        }
      })
    })
  })
}, 1000);