|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + |
| 4 | +<head> |
| 5 | + <meta charset="UTF-8"> |
| 6 | + <meta name="viewport" content="width=device-width,initial-scale=1.0"> |
| 7 | + <title>Event Loop</title> |
| 8 | + <link rel="stylesheet" href="../base.css"> |
| 9 | +</head> |
| 10 | + |
| 11 | +<body> |
| 12 | + <style> |
| 13 | + .go { |
| 14 | + margin: 5rem; |
| 15 | + background: white; |
| 16 | + padding: 5rem; |
| 17 | + width: 25rem; |
| 18 | + height: 25rem; |
| 19 | + transition: all 0.2s; |
| 20 | + } |
| 21 | + |
| 22 | + .go.circle { |
| 23 | + border-radius: 50%; |
| 24 | + } |
| 25 | + |
| 26 | + .go.red { |
| 27 | + background: red; |
| 28 | + } |
| 29 | + |
| 30 | + .go.purple { |
| 31 | + background: purple; |
| 32 | + color: white; |
| 33 | + } |
| 34 | + |
| 35 | + .go.fadeOut { |
| 36 | + opacity: 0; |
| 37 | + } |
| 38 | + </style> |
| 39 | + <div class="go">Click Me</div> |
| 40 | + <script> |
| 41 | + const wait = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms)); |
| 42 | + |
| 43 | + wait(200).then(() => { |
| 44 | + console.log('Done!'); |
| 45 | + }) |
| 46 | + |
| 47 | + const go = document.querySelector('.go'); |
| 48 | + |
| 49 | + |
| 50 | + function animate(e) { |
| 51 | + const el = e.currentTarget; |
| 52 | + // 1. Change the text to GO when clicked. |
| 53 | + el.textContent = 'GO'; |
| 54 | + // 2. Make it a circle after 2 seconds |
| 55 | + wait(200) |
| 56 | + .then(() => { |
| 57 | + el.classList.add('circle'); |
| 58 | + return wait(500); |
| 59 | + }) |
| 60 | + .then(() => { |
| 61 | + // 3. Make it red after 0.5s |
| 62 | + el.classList.add('red'); |
| 63 | + return wait(250); |
| 64 | + }) |
| 65 | + .then(() => { |
| 66 | + el.classList.remove('circle'); |
| 67 | + return wait(500); |
| 68 | + }) |
| 69 | + .then(() => { |
| 70 | + el.classList.remove('red'); |
| 71 | + el.classList.add('purple'); |
| 72 | + return wait(500); |
| 73 | + }) |
| 74 | + .then(() => { |
| 75 | + el.classList.add('fadeOut'); |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + go.addEventListener('click', animate); |
| 80 | + |
| 81 | + go.addEventListener('clickXXXX', function go(e) { |
| 82 | + const el = e.currentTarget; |
| 83 | + // 1. Change the text to GO when clicked. |
| 84 | + el.textContent = 'GO'; |
| 85 | + setTimeout(function () { |
| 86 | + // 2. Make it a circle after 2 seconds |
| 87 | + el.classList.add('circle'); |
| 88 | + setTimeout(function () { |
| 89 | + // 3. Make it red after 0.5s |
| 90 | + el.classList.add('red'); |
| 91 | + setTimeout(function () { |
| 92 | + // 4. make it square after 0.25s |
| 93 | + el.classList.remove('circle'); |
| 94 | + setTimeout(function () { |
| 95 | + // 5. make it purple |
| 96 | + el.classList.remove('red'); |
| 97 | + el.classList.add('purple'); |
| 98 | + setTimeout(function () { |
| 99 | + // 6. fade out after 0.5s |
| 100 | + el.classList.add('invisible'); |
| 101 | + setTimeout(function () { |
| 102 | + console.log('You have reacted the 7th layer of callback hell'); |
| 103 | + el.classList.remove('invisible', 'purple'); |
| 104 | + }, 500); |
| 105 | + }, 500); |
| 106 | + }, 500); |
| 107 | + }, 500) |
| 108 | + }, 500) |
| 109 | + }, 500) |
| 110 | + }); |
| 111 | + |
| 112 | + |
| 113 | + </script> |
| 114 | +</body> |
| 115 | + |
| 116 | +</html> |
0 commit comments