|
8 | 8 | | [Handling Scrolling](#handling-scrolling) | |
9 | 9 | | [Working with `<template>` tag](#working-with-template-tag) | |
10 | 10 | | [Loading Script Dynamically](#loading-script-dynamically) | |
| 11 | +| [Setting Timers and Intervals](#setting-timers-and-intervals) | |
11 | 12 |
|
12 | 13 | Below topics will be covered and for its practical application, [Planner Project](project_planner/) will be used as reference where concept of classes and functions are demonstrated: |
13 | 14 |
|
@@ -229,3 +230,41 @@ In this example, we add an event listener for the `load` event on the `script` e |
229 | 230 | > **Note** |
230 | 231 | > |
231 | 232 | > Adding scripts dynamically can be a powerful feature, but it's important to use it carefully. If you're dynamically rendering user-created scripts, you need to be especially cautious, as this could make your website vulnerable to attacks like cross-site scripting. This type of attack involves injecting malicious code into your website's code, and we'll be discussing it in more detail in the security section later in the course. To avoid these kinds of attacks, it's crucial that you don't dynamically add a script based on user-entered content, or if you must do so, be sure to validate and sanitize the content thoroughly before executing it. |
| 233 | +
|
| 234 | +## Setting Timers and Intervals |
| 235 | + |
| 236 | +You can use timers and intervals to execute code after a set amount of time has elapsed. Here's how you can set a timer and an interval using the DOM: |
| 237 | + |
| 238 | +### Setting a timer using [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) |
| 239 | + |
| 240 | +```javascript |
| 241 | +// Set a timer to execute some code after 3 seconds |
| 242 | +const timer = setTimeout(() => { |
| 243 | + // Code to be executed after 3 seconds |
| 244 | +}, 3000); |
| 245 | +``` |
| 246 | + |
| 247 | +In this example, `setTimeout` is used to set a timer that will execute the function after 3 seconds (3000 milliseconds) have elapsed. The function can contain any code you want to execute after the specified time. |
| 248 | + |
| 249 | +### Setting an interval using [`setInterval`](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) |
| 250 | + |
| 251 | +```javascript |
| 252 | +// Set an interval to execute some code every 2 seconds |
| 253 | +const interval = setInterval(() => { |
| 254 | + // Code to be executed every 2 seconds |
| 255 | +}, 2000); |
| 256 | +``` |
| 257 | + |
| 258 | +In this example, `setInterval` is used to set an interval that will execute the function every 2 seconds (2000 milliseconds). Again, the function can contain any code you want to execute at regular intervals. |
| 259 | + |
| 260 | +To clear a timer or interval, you can use the `clearTimeout` or `clearInterval` methods respectively. For example: |
| 261 | + |
| 262 | +```javascript |
| 263 | +// Clear the timer |
| 264 | +clearTimeout(timer); |
| 265 | + |
| 266 | +// Clear the interval |
| 267 | +clearInterval(interval); |
| 268 | +``` |
| 269 | + |
| 270 | +In this example, `clearTimeout` is used to cancel the timer and prevent the code from executing after the specified time. `clearInterval` works similarly for intervals. |
0 commit comments