如何在延迟一段时间后召回一个计时器

时间:2017-08-21 08:58:33

标签: javascript jquery

我试图在延迟一段时间后重新启动计时器。我如何进行回调?请帮忙......



$sql_query_all = "SELECT SUM(pgp) AS pgp_all, SUM(pfp) AS pfp_all, SUM(klt) AS klt_all, SUM(halbe) AS halbe_all, SUM(viertel) AS viertel_all FROM tauschpartner WHERE ID != 1";
$sql = mysqli_query($mysqli, $sql_query_all);
   while($row_all = mysqli_fetch_array($sql)) {  // see here
      $pgp_bestand_all     = $row_all['pgp_all'];  // change $row to $row_all
      $pfp_bestand_all     = $row_all['pfp_all'];
      $klt_bestand_all     = $row_all['klt_all'];
      $halbe_bestand_all   = $row_all['halbe_all'];
      $viertel_bestand_all = $row_all['viertel_all'];
    };

var i = 0;
var createInterval = setInterval(function() {
    $('#counter').html(++i);

    //delay
    setTimeout(function() {
        return createInterval;
    }, 10000);

    if (i === 10) {
        clearInterval(createInterval);
    }
}, 1000);




JS FIDDLE

5 个答案:

答案 0 :(得分:0)

    var i = 0;

    function A() {
        $('#counter').html(++i);
    
        if (i === 10) {
            clearInterval(createInterval);
        }
        
        //delay
        setTimeout(function() {
            A();
        }, 10000);
    }
    var createInterval = setInterval(A, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="counter">0</div>

试试这段代码

它创建一个函数A(),它将以特定的间隔调用,在setTimeout()中以递归方式调用;

答案 1 :(得分:0)

var i = 0;
setTimeout(myFunction, 1000);
function myFunction() {
    $('#counter').html(++i);
    if (i < 10) {
        setTimeout(myFunction, 10000);
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="counter"></div>

使用两个setTimeout。一段时间后调用相同的函数。

答案 2 :(得分:0)

setTimeout是一个异步函数,它不会阻塞你的代码,但会在你提供的延迟后执行该函数。 setInterval将每隔X毫秒执行一次代码,直到您使用clearInterval停止它。请参阅下面的示例:

var i = 0;

function increment () {
  $('#counter').html(++i);
  
  if (i === 10) {
    clearInterval(interval);
  }
}

var interval = setInterval(increment, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="counter">0</div>

答案 3 :(得分:0)

要在延迟一段时间后启动计时器,首先创建一个递增计时器的函数,然后在X秒后调用它,并在每隔Y秒后执行setInterval,并使用clearInterval清除由https://www.w3schools.com/jsref/met_win_clearinterval.asp <上显示的setInterval设置的计时器/ p>

检查此代码,演示上述陈述 -

var i = 0;
var createInterval = function() {
  var timer = setInterval(function() {
    $('#counter').html(++i);
    if (i === 10) {
      $('#text').html('Stop');
      clearInterval(timer);
    }
  }, 1000);
}

$('#text').html('Waiting... for 5 sec to start timer');

setTimeout(function() {
  $('#text').html('Start...');
  createInterval();
}, 5000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="text"></div>
<div id="counter"></div>

希望,这对你有用.. :) :)

答案 4 :(得分:0)

谢谢大家的宝贵时间。也许我对我的问题不够清楚。

<div id="counter"></div>

(function product_timer() {
  var product_interval = 20, //20 seconds
    delay = 10000; //10 seconds
  //product interval
  productInterval = setInterval(function() {
    product_interval--;
    $('#counter').html(product_interval+'s');
    if (product_interval === 0) {
      $('#counter').html('Please Wait...');
      clearInterval(productInterval);
      setTimeout(function() {
        //delay and callback product timer
        product_timer();
      }, delay); // 10 s
    }
  }, 1000);
}());

FIDDLE