JavaScript Leave a loop after 2 seonds

JavaScript is single threaded. This means that as long as your loop runs, your timeout will never be fired.

Depending on what you want and whether or not you want to lock down your browser (by using an actual infinite loop), you can use the setInterval as a loop, and use the setTimeout to stop the interval after 2 seconds.

console.log("Starting loop");

var interval = setInterval(function () {
    console.log("Let's repeat for 2 seconds...");
}, 0);

setTimeout(function() {
    clearInterval(interval);
    console.log("Finished loop");
}, 2000);
Answer:1

Additionaly to the other answer you could just check the time instead:

 const start = +new Date;

  while(+new Date < start + 2000) {
     console.log("Let's repeat for 2 seconds...");
 }
Answer:2

There is no overflow property for parent elements. The parent element has a set height. The navigation bar simply won't be sticky no matter what I try. It doesn't work with JavaScript either. I must ...

There is no overflow property for parent elements. The parent element has a set height. The navigation bar simply won't be sticky no matter what I try. It doesn't work with JavaScript either. I must ...

  1. isn't
  2. https://www.instagram.com/
  3. isnetworld
  4. instagram
  5. isn't she lovely
  6. isnt it romantic
  7. isna
  8. isn't it ironic
  9. isner
  10. isnull sql
  11. isn network
  12. isnac 2020
  13. isnt she lovely chords
  14. isns
  15. isnan
  16. isn't it
  17. isnull
  18. isn't it romantic cast
  19. isna excel
  20. disney plus

Hello, everybody, I have this task: I have an array [4,7,3,6,9] and I have to make an array like this: [4,7,3,6,9] [9,4,7,3,6] [6,9,4,7,3] [3,6,9,4,7] [7,3,6,9,4] I have to make a program where ...

Hello, everybody, I have this task: I have an array [4,7,3,6,9] and I have to make an array like this: [4,7,3,6,9] [9,4,7,3,6] [6,9,4,7,3] [3,6,9,4,7] [7,3,6,9,4] I have to make a program where ...

  1. javascript rotate array 90 degrees
  2. javascript rotate array left
  3. javascript rotate array elements
  4. javascript rotate array values
  5. javascript rotate array
  6. javascript rotate array right
  7. js rotate array
  8. js rotate array elements
  9. javascript rotate 2d array
  10. javascript rotate through array
  11. javascript rotate multidimensional array
  12. js rotate 2d array
  13. javascript rotate 2 dimensional array

I'm trying to get cookies from my Chrome extension when the app is loading: chrome.cookies.getAll({ "url": config.cookie.cookieUrl }, function (cookies) { if (callback) callback(cookies); ...

I'm trying to get cookies from my Chrome extension when the app is loading: chrome.cookies.getAll({ "url": config.cookie.cookieUrl }, function (cookies) { if (callback) callback(cookies); ...

  1. chrome extension canned responses
  2. chrome extension cannot be removed
  3. chrome extension canvas
  4. chrome extension cannot access contents of url
  5. chrome extension cantonese input
  6. chrome extension canvas defender
  7. chrome extension cannot move into directory
  8. chrome extension cannot be added from this website
  9. chrome extension cancel request
  10. chrome extension cannot be installed from this website
  11. chrome extension canonical
  12. chrome extension cannot unzip
  13. chrome extension candy crush cheat
  14. chrome extension cannot enable
  15. chrome extension canada vpn
  16. chrome extension can't remove
  17. chrome extension cantonese
  18. chrome extension cannot open

I'm very new to web development so have mercy with your responses. I have a grid of images that I want to modify. When the mouse hovers over an image, I want an overlay with text to appear over the ...

I'm very new to web development so have mercy with your responses. I have a grid of images that I want to modify. When the mouse hovers over an image, I want an overlay with text to appear over the ...

  1. overlay image with text
  2. overlay image with color css
  3. overlay image with grid
  4. overlay image with another image
  5. overlay image with transparency
  6. overlay image with gif
  7. overlay image with color online
  8. overlay image with gradient css
  9. overlay image with div
  10. overlay image with button
  11. overlay image with css
  12. overlay image with text bootstrap
  13. overlay image with svg
  14. overlay image with text online
  15. overlay image with bootstrap
  16. overlay image with javascript
  17. overlay with image
  18. image overlay with icon
  19. image overlay with link
  20. image overlay with flexbox