{"id":10388,"date":"2023-06-13T18:04:45","date_gmt":"2023-06-13T18:04:45","guid":{"rendered":"https:\/\/basescripts.com\/?p=10388"},"modified":"2023-06-13T18:04:51","modified_gmt":"2023-06-13T18:04:51","slug":"javascript-set-interval","status":"publish","type":"post","link":"https:\/\/basescripts.com\/javascript-set-interval","title":{"rendered":"JavaScript set Interval"},"content":{"rendered":"\n

In JavaScript, setInterval()<\/code> is a method used to repeatedly execute a function or a piece of code at a specified time interval. It takes two parameters: the function to be executed and the time delay (in milliseconds) between each execution.<\/p>\n\n\n\n

Here’s an example that demonstrates the usage of setInterval()<\/code>:<\/p>\n\n\n\n

\/\/ Define a function to be executed\nfunction greet() {\n  console.log(\"Hello!\");\n}\n\n\/\/ Call setInterval and pass the function and time delay\nvar intervalId = setInterval(greet, 1000); \/\/ Executes the function every 1000 milliseconds (1 second)\n<\/code><\/pre>\n\n\n\n

In this example, the greet()<\/code> function will be executed every 1000 milliseconds (1 second). The setInterval()<\/code> function returns an interval ID that can be used to later stop the execution using the clearInterval()<\/code> method.<\/p>\n\n\n\n

Here’s an example that shows how to stop the execution using clearInterval()<\/code>:<\/p>\n\n\n\n

\/\/ Define a function to be executed\nfunction greet() {\n  console.log(\"Hello!\");\n}\n\n\/\/ Call setInterval and store the interval ID\nvar intervalId = setInterval(greet, 1000); \/\/ Executes the function every 1000 milliseconds (1 second)\n\n\/\/ Stop the execution after 5 seconds (5000 milliseconds)\nsetTimeout(function() {\n  clearInterval(intervalId);\n}, 5000);\n<\/code><\/pre>\n\n\n\n

In this updated example, the setInterval()<\/code> function is called to execute the greet()<\/code> function every second. However, after 5 seconds (5000 milliseconds), the clearInterval()<\/code> function is called with the interval ID to stop the execution.<\/p>\n\n\n\n

Remember to use clearInterval()<\/code> to stop the interval when you no longer need it, as failing to do so may lead to unnecessary resource consumption and unwanted behavior in your code.<\/p>\n\n\n\n

\"\"<\/figure>\n","protected":false},"excerpt":{"rendered":"

In JavaScript, setInterval() is a method used to repeatedly execute a function or a piece of code at a specified time interval. It takes two parameters: the function to be executed and the time delay (in milliseconds) between each execution. Here’s an example that demonstrates the usage of setInterval(): \/\/ Define a function to be … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":10389,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-10388","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/06\/image-6.png?fit=1172%2C358&ssl=1","jetpack-related-posts":[{"id":10744,"url":"https:\/\/basescripts.com\/javascript-code-example-create-a-digital-clock","url_meta":{"origin":10388,"position":0},"title":"JavaScript code Example Create a Digital Clock","author":"Laurence Svekis","date":"August 31, 2023","format":false,"excerpt":"Project: Digital Clock Step 1: HTML Structure Create an HTML file named index.html and set up the basic structure. \u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0Digital Clock<\/title> \u00a0\u00a0\u00a0\u00a0<link rel=\"stylesheet\" href=\"styles.css\"> <\/head> <body> \u00a0\u00a0\u00a0\u00a0<div class=\"container\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<h1>Digital Clock<\/h1> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<p id=\"time\"><\/p> \u00a0\u00a0\u00a0\u00a0<\/div> \u00a0\u00a0\u00a0\u00a0<script src=\"script.js\"><\/script> <\/body> <\/html> Step 2:\u2026","rel":"","context":"In "JavaScript"","block_context":{"text":"JavaScript","link":"https:\/\/basescripts.com\/category\/javascript"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/08\/digitalclock.jpg?fit=1200%2C512&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/08\/digitalclock.jpg?fit=1200%2C512&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/08\/digitalclock.jpg?fit=1200%2C512&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/08\/digitalclock.jpg?fit=1200%2C512&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/08\/digitalclock.jpg?fit=1200%2C512&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":5362,"url":"https:\/\/basescripts.com\/javascript-lesson-start-and-stop-timer-gettime-for-timer-using-date","url_meta":{"origin":10388,"position":1},"title":"JavaScript Lesson Start and Stop Timer GetTime For Timer using Date","author":"Laurence Svekis","date":"May 4, 2022","format":false,"excerpt":"GetTime For Timer using DateHow to set up a timer based on a JavaScript Date object. This project will create a set of buttons that can start and stop a timer. The timer values are based on the ECMAScript epoch from the Date object in JavaScript that can be returned\u2026","rel":"","context":"In "Course Coupons"","block_context":{"text":"Course Coupons","link":"https:\/\/basescripts.com\/category\/course-coupons"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2022\/05\/timer2.jpg?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2022\/05\/timer2.jpg?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2022\/05\/timer2.jpg?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2022\/05\/timer2.jpg?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2022\/05\/timer2.jpg?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":24030,"url":"https:\/\/basescripts.com\/10-javascript-exercises","url_meta":{"origin":10388,"position":2},"title":"10 JavaScript Exercises","author":"Laurence Svekis","date":"November 29, 2025","format":false,"excerpt":"1. Sum of Two Numbers (Basics: Variables, Input, Output) Learning Objectives Declare and use variables Get user input with prompt Convert strings to numbers Show output with alert or console.log Task Ask the user for two numbers, add them, and show the result. Sample Solution const num1 = Number(prompt(\"Enter the\u2026","rel":"","context":"In "JavaScript"","block_context":{"text":"JavaScript","link":"https:\/\/basescripts.com\/category\/javascript"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2025\/11\/image-26.png?fit=800%2C1200&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2025\/11\/image-26.png?fit=800%2C1200&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2025\/11\/image-26.png?fit=800%2C1200&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2025\/11\/image-26.png?fit=800%2C1200&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":10771,"url":"https:\/\/basescripts.com\/javascript-code-dynamic-background-images","url_meta":{"origin":10388,"position":3},"title":"JavaScript Code Dynamic Background Images","author":"Laurence Svekis","date":"August 31, 2023","format":false,"excerpt":"Dynamic Background Changer project. This project will create a web page that cycles through captivating background images at a set interval. Here's a step-by-step breakdown along with the full JavaScript code and description. Project: Dynamic Background Changer Step 1: HTML Structure Create an HTML file named index.html and set up\u2026","rel":"","context":"In "JavaScript"","block_context":{"text":"JavaScript","link":"https:\/\/basescripts.com\/category\/javascript"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/08\/image-21.png?fit=1200%2C510&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/08\/image-21.png?fit=1200%2C510&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/08\/image-21.png?fit=1200%2C510&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/08\/image-21.png?fit=1200%2C510&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/08\/image-21.png?fit=1200%2C510&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":7603,"url":"https:\/\/basescripts.com\/javascript-interview-questions-with-examples-of-code-and-code-snippets-2023","url_meta":{"origin":10388,"position":4},"title":"JavaScript interview questions with Examples of Code and Code Snippets 2023","author":"Laurence Svekis","date":"January 30, 2023","format":false,"excerpt":"JavaScript interview questions with answers: What is closure in JavaScript and how does it work? Answer: Closure is a feature in JavaScript where a function has access to its outer scope even after the outer function has returned. It is created when a function is defined inside another function, and\u2026","rel":"","context":"In "JavaScript"","block_context":{"text":"JavaScript","link":"https:\/\/basescripts.com\/category\/javascript"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/01\/JS14.jpg?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/01\/JS14.jpg?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/01\/JS14.jpg?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/01\/JS14.jpg?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/01\/JS14.jpg?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":10051,"url":"https:\/\/basescripts.com\/explain-the-concept-of-debouncing-and-throttling-in-javascript","url_meta":{"origin":10388,"position":5},"title":"Explain the concept of debouncing and throttling in JavaScript.","author":"Laurence Svekis","date":"May 8, 2023","format":false,"excerpt":"Debouncing and throttling are techniques used in JavaScript to control the frequency of executing a particular function in response to an event. They help optimize performance and improve user experience in scenarios where frequent or rapid event triggering can lead to unnecessary function executions. Debouncing: Debouncing is the process of\u2026","rel":"","context":"In "JavaScript"","block_context":{"text":"JavaScript","link":"https:\/\/basescripts.com\/category\/javascript"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/basescripts.com\/wp-content\/uploads\/2023\/05\/image-31.png?fit=447%2C466&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pcGry9-2Hy","_links":{"self":[{"href":"https:\/\/basescripts.com\/wp-json\/wp\/v2\/posts\/10388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/basescripts.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/basescripts.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/basescripts.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/basescripts.com\/wp-json\/wp\/v2\/comments?post=10388"}],"version-history":[{"count":1,"href":"https:\/\/basescripts.com\/wp-json\/wp\/v2\/posts\/10388\/revisions"}],"predecessor-version":[{"id":10390,"href":"https:\/\/basescripts.com\/wp-json\/wp\/v2\/posts\/10388\/revisions\/10390"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/basescripts.com\/wp-json\/wp\/v2\/media\/10389"}],"wp:attachment":[{"href":"https:\/\/basescripts.com\/wp-json\/wp\/v2\/media?parent=10388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/basescripts.com\/wp-json\/wp\/v2\/categories?post=10388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/basescripts.com\/wp-json\/wp\/v2\/tags?post=10388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}