Timers


In this lesson and in next lesson we are discuss about timers and animations but before taking in use of animation we have to learn perfectly with timers or its functionality. So in this lessons we see two main timer methods are
1. setTimeout : This function set delay for another function.
2. setInterval :
(They are global window functions so we can use it without using or write window object.)
let see an example of timeout function:

setTimeout : is for defer to execution of function.

Timer function

In this above example we create first a var delayTime to setting how much time we want to delay our function for display and we set here value 2000, this time is in milliseconds so that's why we set 2000 (2000 milliseconds = 2 seconds). Than we create our function and we give it a name to function is doSomething. In function body we set a display message, you can write anything in message. After closing the body of doSomething function use setTimeout method, put doSomething function inside because we want to delay doSomething function and than we put delayTime var which we set 2 seconds. Finally our function is ready for use. It's display message on console screen 2 seconds delay every time when we refresh the page. Just copy the example and check out yourself.

clearTimeout : for stop setTimeout function.

Timer Loop
In above example 1. we set delay timer.
2. create a var i = 0 .
3. create a function name doSomething.
4. create message for display how many time this function executed and set inside i+1 value who able to counting.
5. increase ever time one value of i.
6. set condition run loop till i is less than 10.
7. set timeout method.
8. closing function doSomething body and condition statement body.
9. set a timer var for timeout method.
10. in last line we set a clearTimeout method if we uncomment it than our function is not going to work. ClearTimeout method is use for clear or disable all functionality of setTimeout function.

setInterval : repeatedly call a function with time delay | clearInterval :stop setInterval function

setInterval function
1. Set delay speed 200 milliseconds.
2. Create var i = 0 (for starting loop with 0).
3. Set doSomething function.
4. Display message with counting every time.
5. Increase one value of i every time.
6. Set condition that run loop till i > 9.
7. Setting up clearInterval function, passing into setInterval function who we store into vat time.
8. Create var time for store values of setInterval function.


Wconcert India © 2015   ·   All Rights Reserved

Free Web Hosting