1# Timer<a name="EN-US_TOPIC_0000001115814848"></a> 2 3- [Module to Import](#en-us_topic_0000001058562847_section7480141454919) 4- [Permission List](#en-us_topic_0000001058562847_section11257113618419) 5- [setTimeout\(handler\[, delay\[, ...args\]\]\)](#en-us_topic_0000001058562847_section691616559215) 6- [clearTimeout\(timeoutID\)](#en-us_topic_0000001058562847_section591824471012) 7- [setInterval\(handler\[, delay\[, ...args\]\]\)](#en-us_topic_0000001058562847_section3644185910144) 8- [clearInterval\(intervalID\)](#en-us_topic_0000001058562847_section16451595149) 9 10## Module to Import<a name="en-us_topic_0000001058562847_section7480141454919"></a> 11 12None 13 14## Permission List<a name="en-us_topic_0000001058562847_section11257113618419"></a> 15 16None 17 18## setTimeout\(handler\[, delay\[, ...args\]\]\)<a name="en-us_topic_0000001058562847_section691616559215"></a> 19 20Sets a timer for the system to call a function after the timer goes off. 21 22- Parameters 23 24 <a name="en-us_topic_0000001058562847_t80f094cffa594e7e842eccce3bf1eb19"></a> 25 <table><thead align="left"><tr id="en-us_topic_0000001058562847_rd6c76d49b85e4aea9496fd457b189a2a"><th class="cellrowborder" valign="top" width="14.180000000000001%" id="mcps1.1.5.1.1"><p id="en-us_topic_0000001058562847_aca3ed031fef246189779018bbcab4df3"><a name="en-us_topic_0000001058562847_aca3ed031fef246189779018bbcab4df3"></a><a name="en-us_topic_0000001058562847_aca3ed031fef246189779018bbcab4df3"></a>Name</p> 26 </th> 27 <th class="cellrowborder" valign="top" width="16.61%" id="mcps1.1.5.1.2"><p id="en-us_topic_0000001058562847_a89f9e11cc75c4aa9b0b31af966020f61"><a name="en-us_topic_0000001058562847_a89f9e11cc75c4aa9b0b31af966020f61"></a><a name="en-us_topic_0000001058562847_a89f9e11cc75c4aa9b0b31af966020f61"></a>Type</p> 28 </th> 29 <th class="cellrowborder" valign="top" width="6.3%" id="mcps1.1.5.1.3"><p id="en-us_topic_0000001058562847_a411664c3845840d4b2a6845105703f3b"><a name="en-us_topic_0000001058562847_a411664c3845840d4b2a6845105703f3b"></a><a name="en-us_topic_0000001058562847_a411664c3845840d4b2a6845105703f3b"></a>Mandatory</p> 30 </th> 31 <th class="cellrowborder" valign="top" width="62.91%" id="mcps1.1.5.1.4"><p id="en-us_topic_0000001058562847_ac962681702ac49e09c77a1270af9ecb2"><a name="en-us_topic_0000001058562847_ac962681702ac49e09c77a1270af9ecb2"></a><a name="en-us_topic_0000001058562847_ac962681702ac49e09c77a1270af9ecb2"></a>Description</p> 32 </th> 33 </tr> 34 </thead> 35 <tbody><tr id="en-us_topic_0000001058562847_r9d3ffb29e7e74ff4b18e0a3e3ad04925"><td class="cellrowborder" valign="top" width="14.180000000000001%" headers="mcps1.1.5.1.1 "><p id="en-us_topic_0000001058562847_p1411112320620"><a name="en-us_topic_0000001058562847_p1411112320620"></a><a name="en-us_topic_0000001058562847_p1411112320620"></a>handler</p> 36 </td> 37 <td class="cellrowborder" valign="top" width="16.61%" headers="mcps1.1.5.1.2 "><p id="en-us_topic_0000001058562847_p11016237618"><a name="en-us_topic_0000001058562847_p11016237618"></a><a name="en-us_topic_0000001058562847_p11016237618"></a>Function</p> 38 </td> 39 <td class="cellrowborder" valign="top" width="6.3%" headers="mcps1.1.5.1.3 "><p id="en-us_topic_0000001058562847_p1892231068"><a name="en-us_topic_0000001058562847_p1892231068"></a><a name="en-us_topic_0000001058562847_p1892231068"></a>Yes</p> 40 </td> 41 <td class="cellrowborder" valign="top" width="62.91%" headers="mcps1.1.5.1.4 "><p id="en-us_topic_0000001058562847_p788516491307"><a name="en-us_topic_0000001058562847_p788516491307"></a><a name="en-us_topic_0000001058562847_p788516491307"></a>Function to be called after the timer goes off.</p> 42 </td> 43 </tr> 44 <tr id="en-us_topic_0000001058562847_r57344f1bd4304d548bf54ba81dbe8fa5"><td class="cellrowborder" valign="top" width="14.180000000000001%" headers="mcps1.1.5.1.1 "><p id="en-us_topic_0000001058562847_p13718237614"><a name="en-us_topic_0000001058562847_p13718237614"></a><a name="en-us_topic_0000001058562847_p13718237614"></a>delay</p> 45 </td> 46 <td class="cellrowborder" valign="top" width="16.61%" headers="mcps1.1.5.1.2 "><p id="en-us_topic_0000001058562847_p261239618"><a name="en-us_topic_0000001058562847_p261239618"></a><a name="en-us_topic_0000001058562847_p261239618"></a>number</p> 47 </td> 48 <td class="cellrowborder" valign="top" width="6.3%" headers="mcps1.1.5.1.3 "><p id="en-us_topic_0000001058562847_p17516231062"><a name="en-us_topic_0000001058562847_p17516231062"></a><a name="en-us_topic_0000001058562847_p17516231062"></a>No</p> 49 </td> 50 <td class="cellrowborder" valign="top" width="62.91%" headers="mcps1.1.5.1.4 "><p id="en-us_topic_0000001058562847_p1943234615"><a name="en-us_topic_0000001058562847_p1943234615"></a><a name="en-us_topic_0000001058562847_p1943234615"></a>Number of milliseconds delayed before the execution. If this parameter is left empty, the default value <strong id="en-us_topic_0000001058562847_b1264512254010"><a name="en-us_topic_0000001058562847_b1264512254010"></a><a name="en-us_topic_0000001058562847_b1264512254010"></a>0</strong> is used, which means that the execution starts immediately or as soon as possible.</p> 51 </td> 52 </tr> 53 <tr id="en-us_topic_0000001058562847_r41a55619ed484b7092dfc843ebd4fba1"><td class="cellrowborder" valign="top" width="14.180000000000001%" headers="mcps1.1.5.1.1 "><p id="en-us_topic_0000001058562847_p103182316615"><a name="en-us_topic_0000001058562847_p103182316615"></a><a name="en-us_topic_0000001058562847_p103182316615"></a>...args</p> 54 </td> 55 <td class="cellrowborder" valign="top" width="16.61%" headers="mcps1.1.5.1.2 "><p id="en-us_topic_0000001058562847_p1321231162"><a name="en-us_topic_0000001058562847_p1321231162"></a><a name="en-us_topic_0000001058562847_p1321231162"></a>Array<any></p> 56 </td> 57 <td class="cellrowborder" valign="top" width="6.3%" headers="mcps1.1.5.1.3 "><p id="en-us_topic_0000001058562847_p11111231363"><a name="en-us_topic_0000001058562847_p11111231363"></a><a name="en-us_topic_0000001058562847_p11111231363"></a>No</p> 58 </td> 59 <td class="cellrowborder" valign="top" width="62.91%" headers="mcps1.1.5.1.4 "><p id="en-us_topic_0000001058562847_p139991022863"><a name="en-us_topic_0000001058562847_p139991022863"></a><a name="en-us_topic_0000001058562847_p139991022863"></a>Additional parameter to pass to the handler after the timer goes off.</p> 60 </td> 61 </tr> 62 </tbody> 63 </table> 64 65- Return Value 66 67 **timeoutID**: timer ID 68 69- Example 70 71 ``` 72 var timeoutID = setTimeout(function() { 73 console.log('delay 1s'); 74 }, 1000); 75 ``` 76 77 78## clearTimeout\(timeoutID\)<a name="en-us_topic_0000001058562847_section591824471012"></a> 79 80Cancels the timer created via **setTimeout\(\)**. 81 82- Parameter 83 84 <a name="en-us_topic_0000001058562847_table242593951313"></a> 85 <table><thead align="left"><tr id="en-us_topic_0000001058562847_row1642533921320"><th class="cellrowborder" valign="top" width="14.180000000000001%" id="mcps1.1.5.1.1"><p id="en-us_topic_0000001058562847_p15425539131316"><a name="en-us_topic_0000001058562847_p15425539131316"></a><a name="en-us_topic_0000001058562847_p15425539131316"></a>Name</p> 86 </th> 87 <th class="cellrowborder" valign="top" width="9.700000000000001%" id="mcps1.1.5.1.2"><p id="en-us_topic_0000001058562847_p442518395136"><a name="en-us_topic_0000001058562847_p442518395136"></a><a name="en-us_topic_0000001058562847_p442518395136"></a>Type</p> 88 </th> 89 <th class="cellrowborder" valign="top" width="6.18%" id="mcps1.1.5.1.3"><p id="en-us_topic_0000001058562847_p6425153915136"><a name="en-us_topic_0000001058562847_p6425153915136"></a><a name="en-us_topic_0000001058562847_p6425153915136"></a>Mandatory</p> 90 </th> 91 <th class="cellrowborder" valign="top" width="69.94%" id="mcps1.1.5.1.4"><p id="en-us_topic_0000001058562847_p9425193910134"><a name="en-us_topic_0000001058562847_p9425193910134"></a><a name="en-us_topic_0000001058562847_p9425193910134"></a>Description</p> 92 </th> 93 </tr> 94 </thead> 95 <tbody><tr id="en-us_topic_0000001058562847_row1442613913132"><td class="cellrowborder" valign="top" width="14.180000000000001%" headers="mcps1.1.5.1.1 "><p id="en-us_topic_0000001058562847_p16426133931310"><a name="en-us_topic_0000001058562847_p16426133931310"></a><a name="en-us_topic_0000001058562847_p16426133931310"></a>timeoutID</p> 96 </td> 97 <td class="cellrowborder" valign="top" width="9.700000000000001%" headers="mcps1.1.5.1.2 "><p id="en-us_topic_0000001058562847_p16426639151316"><a name="en-us_topic_0000001058562847_p16426639151316"></a><a name="en-us_topic_0000001058562847_p16426639151316"></a>number</p> 98 </td> 99 <td class="cellrowborder" valign="top" width="6.18%" headers="mcps1.1.5.1.3 "><p id="en-us_topic_0000001058562847_p1426739141312"><a name="en-us_topic_0000001058562847_p1426739141312"></a><a name="en-us_topic_0000001058562847_p1426739141312"></a>Yes</p> 100 </td> 101 <td class="cellrowborder" valign="top" width="69.94%" headers="mcps1.1.5.1.4 "><p id="en-us_topic_0000001058562847_p6426183941319"><a name="en-us_topic_0000001058562847_p6426183941319"></a><a name="en-us_topic_0000001058562847_p6426183941319"></a>ID of the timer to cancel, which is returned by <strong id="en-us_topic_0000001058562847_b5804644564"><a name="en-us_topic_0000001058562847_b5804644564"></a><a name="en-us_topic_0000001058562847_b5804644564"></a>setTimeout()</strong></p> 102 </td> 103 </tr> 104 </tbody> 105 </table> 106 107- Example 108 109 ``` 110 var timeoutID = setTimeout(function() { 111 console.log('do after 1s delay.'); 112 }, 1000); 113 114 clearTimeout(timeoutID); 115 ``` 116 117 118## setInterval\(handler\[, delay\[, ...args\]\]\)<a name="en-us_topic_0000001058562847_section3644185910144"></a> 119 120Sets a repeating timer for the system to repeatedly call a function at a fixed interval. 121 122- Parameters 123 124 <a name="en-us_topic_0000001058562847_table2064413592143"></a> 125 <table><thead align="left"><tr id="en-us_topic_0000001058562847_row16441591142"><th class="cellrowborder" valign="top" width="14.180000000000001%" id="mcps1.1.5.1.1"><p id="en-us_topic_0000001058562847_p0644195915145"><a name="en-us_topic_0000001058562847_p0644195915145"></a><a name="en-us_topic_0000001058562847_p0644195915145"></a>Name</p> 126 </th> 127 <th class="cellrowborder" valign="top" width="16.61%" id="mcps1.1.5.1.2"><p id="en-us_topic_0000001058562847_p1264413594142"><a name="en-us_topic_0000001058562847_p1264413594142"></a><a name="en-us_topic_0000001058562847_p1264413594142"></a>Type</p> 128 </th> 129 <th class="cellrowborder" valign="top" width="6.3%" id="mcps1.1.5.1.3"><p id="en-us_topic_0000001058562847_p86449591147"><a name="en-us_topic_0000001058562847_p86449591147"></a><a name="en-us_topic_0000001058562847_p86449591147"></a>Mandatory</p> 130 </th> 131 <th class="cellrowborder" valign="top" width="62.91%" id="mcps1.1.5.1.4"><p id="en-us_topic_0000001058562847_p16644185991417"><a name="en-us_topic_0000001058562847_p16644185991417"></a><a name="en-us_topic_0000001058562847_p16644185991417"></a>Description</p> 132 </th> 133 </tr> 134 </thead> 135 <tbody><tr id="en-us_topic_0000001058562847_row264495931412"><td class="cellrowborder" valign="top" width="14.180000000000001%" headers="mcps1.1.5.1.1 "><p id="en-us_topic_0000001058562847_p8644659161413"><a name="en-us_topic_0000001058562847_p8644659161413"></a><a name="en-us_topic_0000001058562847_p8644659161413"></a>handler</p> 136 </td> 137 <td class="cellrowborder" valign="top" width="16.61%" headers="mcps1.1.5.1.2 "><p id="en-us_topic_0000001058562847_p7645259181410"><a name="en-us_topic_0000001058562847_p7645259181410"></a><a name="en-us_topic_0000001058562847_p7645259181410"></a>Function</p> 138 </td> 139 <td class="cellrowborder" valign="top" width="6.3%" headers="mcps1.1.5.1.3 "><p id="en-us_topic_0000001058562847_p12645259111415"><a name="en-us_topic_0000001058562847_p12645259111415"></a><a name="en-us_topic_0000001058562847_p12645259111415"></a>Yes</p> 140 </td> 141 <td class="cellrowborder" valign="top" width="62.91%" headers="mcps1.1.5.1.4 "><p id="en-us_topic_0000001058562847_p4645155931418"><a name="en-us_topic_0000001058562847_p4645155931418"></a><a name="en-us_topic_0000001058562847_p4645155931418"></a>Function to be called repeatedly</p> 142 </td> 143 </tr> 144 <tr id="en-us_topic_0000001058562847_row264575911146"><td class="cellrowborder" valign="top" width="14.180000000000001%" headers="mcps1.1.5.1.1 "><p id="en-us_topic_0000001058562847_p17645159201410"><a name="en-us_topic_0000001058562847_p17645159201410"></a><a name="en-us_topic_0000001058562847_p17645159201410"></a>delay</p> 145 </td> 146 <td class="cellrowborder" valign="top" width="16.61%" headers="mcps1.1.5.1.2 "><p id="en-us_topic_0000001058562847_p3645175951416"><a name="en-us_topic_0000001058562847_p3645175951416"></a><a name="en-us_topic_0000001058562847_p3645175951416"></a>number</p> 147 </td> 148 <td class="cellrowborder" valign="top" width="6.3%" headers="mcps1.1.5.1.3 "><p id="en-us_topic_0000001058562847_p1645185981412"><a name="en-us_topic_0000001058562847_p1645185981412"></a><a name="en-us_topic_0000001058562847_p1645185981412"></a>No</p> 149 </td> 150 <td class="cellrowborder" valign="top" width="62.91%" headers="mcps1.1.5.1.4 "><p id="en-us_topic_0000001058562847_p13645559141415"><a name="en-us_topic_0000001058562847_p13645559141415"></a><a name="en-us_topic_0000001058562847_p13645559141415"></a>Number of milliseconds delayed before the execution</p> 151 </td> 152 </tr> 153 <tr id="en-us_topic_0000001058562847_row96452594148"><td class="cellrowborder" valign="top" width="14.180000000000001%" headers="mcps1.1.5.1.1 "><p id="en-us_topic_0000001058562847_p4645105911412"><a name="en-us_topic_0000001058562847_p4645105911412"></a><a name="en-us_topic_0000001058562847_p4645105911412"></a>...args</p> 154 </td> 155 <td class="cellrowborder" valign="top" width="16.61%" headers="mcps1.1.5.1.2 "><p id="en-us_topic_0000001058562847_p1564595951413"><a name="en-us_topic_0000001058562847_p1564595951413"></a><a name="en-us_topic_0000001058562847_p1564595951413"></a>Array<any></p> 156 </td> 157 <td class="cellrowborder" valign="top" width="6.3%" headers="mcps1.1.5.1.3 "><p id="en-us_topic_0000001058562847_p12645175971411"><a name="en-us_topic_0000001058562847_p12645175971411"></a><a name="en-us_topic_0000001058562847_p12645175971411"></a>No</p> 158 </td> 159 <td class="cellrowborder" valign="top" width="62.91%" headers="mcps1.1.5.1.4 "><p id="en-us_topic_0000001058562847_p12645105991413"><a name="en-us_topic_0000001058562847_p12645105991413"></a><a name="en-us_topic_0000001058562847_p12645105991413"></a>Additional parameter to pass to the handler after the timer goes off</p> 160 </td> 161 </tr> 162 </tbody> 163 </table> 164 165- Return Value 166 167 **intervalID**: ID of the repeating timer 168 169- Example 170 171 ``` 172 var intervalID = setInterval(function() { 173 console.log('do very 1s.'); 174 }, 1000); 175 ``` 176 177 178## clearInterval\(intervalID\)<a name="en-us_topic_0000001058562847_section16451595149"></a> 179 180Cancels the repeating timer set via **setInterval\(\)**. 181 182- Parameter 183 184 <a name="en-us_topic_0000001058562847_table86463597147"></a> 185 <table><thead align="left"><tr id="en-us_topic_0000001058562847_row146462597148"><th class="cellrowborder" valign="top" width="14.180000000000001%" id="mcps1.1.5.1.1"><p id="en-us_topic_0000001058562847_p176461759111416"><a name="en-us_topic_0000001058562847_p176461759111416"></a><a name="en-us_topic_0000001058562847_p176461759111416"></a>Name</p> 186 </th> 187 <th class="cellrowborder" valign="top" width="9.700000000000001%" id="mcps1.1.5.1.2"><p id="en-us_topic_0000001058562847_p8646195918141"><a name="en-us_topic_0000001058562847_p8646195918141"></a><a name="en-us_topic_0000001058562847_p8646195918141"></a>Type</p> 188 </th> 189 <th class="cellrowborder" valign="top" width="6.18%" id="mcps1.1.5.1.3"><p id="en-us_topic_0000001058562847_p19646165981415"><a name="en-us_topic_0000001058562847_p19646165981415"></a><a name="en-us_topic_0000001058562847_p19646165981415"></a>Mandatory</p> 190 </th> 191 <th class="cellrowborder" valign="top" width="69.94%" id="mcps1.1.5.1.4"><p id="en-us_topic_0000001058562847_p8646185961419"><a name="en-us_topic_0000001058562847_p8646185961419"></a><a name="en-us_topic_0000001058562847_p8646185961419"></a>Description</p> 192 </th> 193 </tr> 194 </thead> 195 <tbody><tr id="en-us_topic_0000001058562847_row12646125914143"><td class="cellrowborder" valign="top" width="14.180000000000001%" headers="mcps1.1.5.1.1 "><p id="en-us_topic_0000001058562847_p1964675951413"><a name="en-us_topic_0000001058562847_p1964675951413"></a><a name="en-us_topic_0000001058562847_p1964675951413"></a>intervalID</p> 196 </td> 197 <td class="cellrowborder" valign="top" width="9.700000000000001%" headers="mcps1.1.5.1.2 "><p id="en-us_topic_0000001058562847_p1864625921419"><a name="en-us_topic_0000001058562847_p1864625921419"></a><a name="en-us_topic_0000001058562847_p1864625921419"></a>number</p> 198 </td> 199 <td class="cellrowborder" valign="top" width="6.18%" headers="mcps1.1.5.1.3 "><p id="en-us_topic_0000001058562847_p3646145921419"><a name="en-us_topic_0000001058562847_p3646145921419"></a><a name="en-us_topic_0000001058562847_p3646145921419"></a>Yes</p> 200 </td> 201 <td class="cellrowborder" valign="top" width="69.94%" headers="mcps1.1.5.1.4 "><p id="en-us_topic_0000001058562847_p7646459141416"><a name="en-us_topic_0000001058562847_p7646459141416"></a><a name="en-us_topic_0000001058562847_p7646459141416"></a>ID of the repeating timer to cancel, which is returned by <strong id="en-us_topic_0000001058562847_b1855918269134"><a name="en-us_topic_0000001058562847_b1855918269134"></a><a name="en-us_topic_0000001058562847_b1855918269134"></a>setInterval()</strong>.</p> 202 </td> 203 </tr> 204 </tbody> 205 </table> 206 207- Example 208 209 ``` 210 var intervalID = setInterval(function() { 211 console.log('do very 1s.'); 212 }, 1000); 213 214 clearInterval(intervalID); 215 ``` 216 217 218