JS custom timer and JS custom Timer

Source: Internet
Author: User

JS custom timer and JS custom Timer

Sometimes, for the reason of demand, we need to write a method in JS, and then let it execute at a certain time, that is, we need to write a timer in JS, when the time to achieve the required time, the need to execute the method of automatic execution, the following small make up a simple description of how I achieve (reprint please indicate the reprint address: http://blog.csdn.net/u012116457)

Var tMinutes = 0; var tHours = 0; var go; function dingshi (hours, minutes) {tHours = hours; tMinutes = minutes; go = setInterval (run, 3000 );} function run () {var date = new Date (); if (date. getMinutes ()-tMinutes = 0) & (date. getHours ()-tHours = 0) {clearInterval (go); getData (); // method to be executed }}}
In the parameter hours in dingshi, minutes is the start time of the method to be executed. here only the hour and minute are required. You can add the parameter by yourself under specific circumstances, however, be sure to modify the if judgment condition in the run method.

GetData is the method to be executed. It is also modified according to the actual situation. You can call the dingshi method when using it.

It should also be noted that in order to prevent the browser from crashing, the second parameter of setInterval is set to 3000 milliseconds, that is, 3 seconds. If you require precision to seconds, you should change it to 1000 here, otherwise, you may miss the set time.


How to Write a timer in js

Var a = setTimeout (expression, delay time). When the expression is executed once in the delay time, clearTimeout (a); can be used to cancel the expression;
Var a = setInterval (expression, interval), executed once every interval, you can use clearInterval (a) to cancel
 
How to Write a js timer?

Timer:
It is used to execute a program after a specific period of time.

Regular execution in JS, difference between setTimeout and setInterval, and l release method

SetTimeout (Expression, DelayTime): After DelayTime, an Expression is executed. setTimeout is used for a delay period before a certain operation.
SetTimeout ("function", time) sets a timeout object

SetInterval (expression, delayTime). Expression is executed for each DelayTime. It is often used to refresh an expression.
SetInterval ("function", time) sets a timeout object

SetInterval indicates automatic repetition, and setTimeout does not.

ClearTimeout (object) clears the set setTimeout object
ClearInterval (object) clears the set setInterval object
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.