{"id":2134,"date":"2017-08-11T00:27:18","date_gmt":"2017-08-11T00:27:18","guid":{"rendered":"http:\/\/javascript-tutor.net\/?page_id=2134"},"modified":"2017-11-16T23:15:40","modified_gmt":"2017-11-16T23:15:40","slug":"creating-animation-javascript","status":"publish","type":"page","link":"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/","title":{"rendered":"JavaScript Lesson 35: Creating Animation in JavaScript"},"content":{"rendered":"<h3 style=\"text-align: center;\"><strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/\">&lt;Previous Lesson&gt;<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Table of Contents]<\/a>\u00a0<\/strong><\/h3>\n<p>In order to create animation using javascript, we need to use a kind of timer\u00a0so that an object changes\u00a0according to a certain time interval. In Javascript, we use the <strong>setInterval() method <\/strong>as a timer<strong>.<\/strong><\/p>\n<h3>The setInterval() Method<\/h3>\n<p>The setInterval() method calls a function or runs an expression at specified intervals (in milliseconds).\u00a0The syntax is as follows:<\/p>\n<p>setInterval(myfunction, time interval)<em>.<\/em>)<\/p>\n<p>The setInterval method runs a function created by you at a certain time interval in milliseconds. \u00a0For example, \u00a0a time interval of 1000 equals to 1 second. To execute a function only once, after a specified number of milliseconds, we use the setTimeout() method.<\/p>\n<h4>Example 1<\/h4>\n<p>&lt;script&gt;<br \/>\nfunction myFunction() {<br \/>\nsetInterval(function(){ alert(&#8220;Welcome to JavaScript Tutorial&#8221;); }, 1000);<br \/>\n}<br \/>\n&lt;\/script&gt;);<\/p>\n<p>Running this script produces a popup dialog that displays the message\u00a0&#8220;Welcome to JavaScript Tutorial&#8221; every two seconds.<\/p>\n<p><button onclick=\"myFunction()\">Load Message<\/button><br \/>\n<button onclick=\"myFunction2()\">Stop<\/button><br \/>\n<script>\nfunction myFunction() {\n    var1=setInterval(function(){ alert(\"Welcome to JavaScript Tutorial\"); }, 2000);\n}\nfunction myFunction2() {\n    clearInterval(var1);\n}\n<\/script><\/p>\n<p>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"5669011038\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/p>\n<h4>Example 2<\/h4>\n<p>You can also refer to a \u00a0function; Alert &#8220;Hello&#8221; every 3 seconds (3000 milliseconds):<\/p>\n<p>var\u00a0myVar;<\/p>\n<p>function\u00a0myFunction() {<br \/>\nmyVar = setInterval(alertFunc,\u00a03000);<br \/>\n}<\/p>\n<p>function\u00a0alertFunc() {<br \/>\nalert(&#8220;Hello!&#8221;);<br \/>\n}<\/p>\n<p><button onclick=\"myFunction3()\">Load Message<\/button><br \/>\n<script>\nvar\u00a0myVar1;<\/p>\n<p>function\u00a0myFunction3() {\nmyVar1 = setInterval(alertFunc,\u00a02000);\n}<\/p>\n<p>function\u00a0alertFunc() {\nalert(\"Hello!\");\n}\n<\/script><\/p>\n<h4>Example 3<\/h4>\n<p>Display the current time (the setInterval() method will execute the function once every 1 second, just like a digital watch):<\/p>\n<p>var\u00a0myVar = setInterval(function(){ myTimer() },\u00a01000);<\/p>\n<p>function\u00a0myTimer()\u00a0{<br \/>\nvar\u00a0d =\u00a0new\u00a0Date();<br \/>\nvar\u00a0t = d.toLocaleTimeString();<br \/>\ndocument.getElementById(&#8220;demo&#8221;).innerHTML\u00a0= t;<br \/>\n}<br \/>\n<script>\nvar\u00a0myVar = setInterval(function(){ myTimer() },\u00a01000);<\/p>\n<p>function\u00a0myTimer()\u00a0{\nvar\u00a0d =\u00a0new\u00a0Date();\nvar\u00a0t = d.toLocaleTimeString();\ndocument.getElementById(\"demo\").innerHTML\u00a0= t;\n}<\/p>\n<p><\/script><br \/>\n&nbsp;<\/p>\n<h3>The clearInterval Method<\/h3>\n<p>The clearInterval() method clears a timer set with the\u00a0setInterval()\u00a0method.<\/p>\n<p>The ID value returned by setInterval() is used as the parameter for the clearInterval() method.<\/p>\n<p>The syntax is<\/p>\n<p>clearInterval(<i>id_of_setinterval<\/i>)<\/p>\n<h4>Example 4<\/h4>\n<p>To run and stop and popup dialog<\/p>\n<p>&lt;button onclick=&#8221;myFunction()&#8221;&gt;Run it&lt;\/button&gt;&lt;br&gt;<br \/>\n&lt;button onclick=&#8221;myFunction2()&#8221;&gt;Stop it&lt;\/button&gt;<\/p>\n<p>&lt;script&gt;<br \/>\nvar myVar;<\/p>\n<p>function myFunction() {<br \/>\nmyVar = setInterval(alertFunc, 2000);<br \/>\n}<br \/>\nfunction myFunction2() {<br \/>\nclearInterval(myVar);<br \/>\n}<\/p>\n<p>function alertFunc() {<br \/>\nalert(&#8220;Hello!&#8221;);<br \/>\n}<br \/>\n&lt;\/script&gt;<\/p>\n<h4>Example 5<\/h4>\n<p>In this example, the setInterval() method executes the setColor() function once every 300 milliseconds, which will toggle between two background colors. The stopColor function will stop the animation using the clearInterval method.<\/p>\n<p>&lt;button onclick=&#8221;stopColor()&#8221;&gt;Stop Toggling&lt;\/button&gt;<\/p>\n<p>&lt;script&gt;<br \/>\nvar myVar = setInterval(function(){ setColor() }, 300);<\/p>\n<p>function setColor() {<br \/>\nvar x = document.body;<br \/>\nx.style.backgroundColor = x.style.backgroundColor == &#8220;yellow&#8221; ? &#8220;pink&#8221; : &#8220;yellow&#8221;;<br \/>\n}<\/p>\n<p>function stopColor() {<br \/>\nclearInterval(myVar);<br \/>\n}<br \/>\n&lt;\/script&gt;<\/p>\n<h4>Example 6<\/h4>\n<p>This script create a clock. It is stopped by using the clearInterval method<\/p>\n<p>&lt;p&gt;A script on this page starts this clock:&lt;\/p&gt;<\/p>\n<p>&lt;p id=&#8221;demo&#8221;&gt;&lt;\/p&gt;<\/p>\n<p>&lt;button onclick=&#8221;myStopFunction()&#8221;&gt;Stop time&lt;\/button&gt;<\/p>\n<p>&lt;script&gt;<br \/>\nvar myVar = setInterval(function(){ myTimer() }, 1000);<\/p>\n<p>function myTimer() {<br \/>\nvar d = new Date();<br \/>\nvar t = d.toLocaleTimeString();<br \/>\ndocument.getElementById(&#8220;demo&#8221;).innerHTML = t;<br \/>\n}<\/p>\n<p>function myStopFunction() {<br \/>\nclearInterval(myVar);<br \/>\n}<br \/>\n&lt;\/script&gt;<\/p>\n<p>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"5669011038\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/p>\n<h3 style=\"text-align: center;\"><strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/\">&lt;Previous Lesson&gt;<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Table of Contents]<\/a>\u00a0<\/strong><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Previous Lesson&gt;\u00a0[Table of Contents]\u00a0 In order to create animation using javascript, we need to use a kind of timer\u00a0so that an object changes\u00a0according to a certain time interval. In Javascript, we use the setInterval() method as a timer. The setInterval() Method The setInterval() method calls a function or runs an expression at specified intervals (in &hellip; <a href=\"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JavaScript Lesson 35: Creating Animation in JavaScript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":35895,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2134","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Lesson 35: Creating Animation in JavaScript - Learn JavaScript Online | Free Interactive JavaScript Tutorials<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Lesson 35: Creating Animation in JavaScript - Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"og:description\" content=\"&lt;Previous Lesson&gt;\u00a0[Table of Contents]\u00a0 In order to create animation using javascript, we need to use a kind of timer\u00a0so that an object changes\u00a0according to a certain time interval. In Javascript, we use the setInterval() method as a timer. The setInterval() Method The setInterval() method calls a function or runs an expression at specified intervals (in &hellip; Continue reading &quot;JavaScript Lesson 35: Creating Animation in JavaScript&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-16T23:15:40+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/\",\"url\":\"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/\",\"name\":\"JavaScript Lesson 35: Creating Animation in JavaScript - Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/javascript-tutor.net\/#website\"},\"datePublished\":\"2017-08-11T00:27:18+00:00\",\"dateModified\":\"2017-11-16T23:15:40+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/javascript-tutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Lesson 35: Creating Animation in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/javascript-tutor.net\/#website\",\"url\":\"https:\/\/javascript-tutor.net\/\",\"name\":\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"description\":\"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Lesson 35: Creating Animation in JavaScript - Learn JavaScript Online | Free Interactive JavaScript Tutorials","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Lesson 35: Creating Animation in JavaScript - Learn JavaScript Online | Free Interactive JavaScript Tutorials","og_description":"&lt;Previous Lesson&gt;\u00a0[Table of Contents]\u00a0 In order to create animation using javascript, we need to use a kind of timer\u00a0so that an object changes\u00a0according to a certain time interval. In Javascript, we use the setInterval() method as a timer. The setInterval() Method The setInterval() method calls a function or runs an expression at specified intervals (in &hellip; Continue reading \"JavaScript Lesson 35: Creating Animation in JavaScript\"","og_url":"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/","og_site_name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","article_modified_time":"2017-11-16T23:15:40+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/","url":"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/","name":"JavaScript Lesson 35: Creating Animation in JavaScript - Learn JavaScript Online | Free Interactive JavaScript Tutorials","isPartOf":{"@id":"https:\/\/javascript-tutor.net\/#website"},"datePublished":"2017-08-11T00:27:18+00:00","dateModified":"2017-11-16T23:15:40+00:00","breadcrumb":{"@id":"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/javascript-tutor.net\/"},{"@type":"ListItem","position":2,"name":"JavaScript Lesson 35: Creating Animation in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/javascript-tutor.net\/#website","url":"https:\/\/javascript-tutor.net\/","name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/users\/35895"}],"replies":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=2134"}],"version-history":[{"count":24,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2134\/revisions"}],"predecessor-version":[{"id":2513,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2134\/revisions\/2513"}],"wp:attachment":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=2134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}