Calling a function every 60 seconds

后端 未结 13 2062
情书的邮戳
情书的邮戳 2020-11-22 08:24

Using setTimeout() it is possible to launch a function at a specified time:

setTimeout(function, 60000);

But what if I would l

相关标签:
13条回答
  • 2020-11-22 09:18

    In jQuery you can do like this.

    function random_no(){
         var ran=Math.random();
         jQuery('#random_no_container').html(ran);
    }
               
    window.setInterval(function(){
           /// call your function here
          random_no();
    }, 6000);  // Change Interval here to test. For eg: 5000 for 5 sec
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div id="random_no_container">
          Hello. Here you can see random numbers after every 6 sec
    </div>

    0 讨论(0)
  • 2020-11-22 09:19

    You can simply call setTimeout at the end of the function. This will add it again to the event queue. You can use any kind of logic to vary the delay values. For example,

    function multiStep() {
      // do some work here
      blah_blah_whatever();
      var newtime = 60000;
      if (!requestStop) {
        setTimeout(multiStep, newtime);
      }
    }
    
    0 讨论(0)
  • 2020-11-22 09:19

    There are 2 ways to call-

    1. setInterval(function (){ functionName();}, 60000);

    2. setInterval(functionName, 60000);

    above function will call on every 60 seconds.

    0 讨论(0)
  • 2020-11-22 09:21

    function random(number) {
      return Math.floor(Math.random() * (number+1));
    }
    setInterval(() => {
        const rndCol = 'rgb(' + random(255) + ',' + random(255) + ',' + random(255) + ')';//rgb value (0-255,0-255,0-255)
        document.body.style.backgroundColor = rndCol;   
    }, 1000);
    <script src="test.js"></script>
    it changes background color in every 1 second (written as 1000 in JS)

    0 讨论(0)
  • 2020-11-22 09:22

    Use window.setInterval(func, time).

    0 讨论(0)
  • 2020-11-22 09:22

    here we console natural number 0 to ......n (next number print in console every 60 sec.) , using setInterval()

    var count = 0;
    function abc(){
        count ++;
        console.log(count);
    }
    setInterval(abc,60*1000);
    
    0 讨论(0)
提交回复
热议问题