APICloud implements scheduled processing operations

In APICloud development, when we want to process information at intervals, such as 10 seconds to retrieve a message, we can directly use the setInterval function, which is used in the same way as standard JS. The code is as follows: make a stopwatch, add up once a second.

  • The following code
var num = 0;
setInterval(function() {
      num += 1;
      $api.html($api.byId("textId"), num);
}, 1000);
Copy the code

It can also be written like this to make it easier to understand:

var num = 0;
function addM() {
     num += 1;
     $api.html($api.byId("textId"), num);
 }
 setInterval("addM()".1000);
Copy the code

SetInterval (addM, 1000); You can do that but you can’t say setInterval(addM(), 1000); It’s not going to work

  • Preview the results

  • The complete code
<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" The content = "maximum - scale = 1.0, the minimum - scale = 1.0, user - scalable = 0, width = device - width, initial - scale = 1.0" / > < meta Name ="format-detection" content="telephone=no,email=no,date=no,address=no"> <title> message Frame</title> <link rel="stylesheet" type="text/css" href=".. /.. /css/api.css" /> <style> body {} html, body { height: 100% } body { background-color: #fff; margin: 0; } < / style > < / head > < body > < div id = "wrap" > < div class = "" > timer test < / div > < div class =" "id =" textId "> < / div > < / div > < / body > <script type="text/javascript" src=".. /.. /script/api.js"></script> <script type="text/javascript" src=".. /.. /script/common.js"></script> <script type="text/javascript"> apiready = function() { } var num = 0; function addM() { num += 1; $api.html($api.byId("textId"), num); } setInterval("addM()", 1000); </script> </html>Copy the code