Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.  
  3.     'use strict';
  4.  
  5.     var section = document.getElementsByTagName('section')[0];
  6.     var sectionInitialColor = window.getComputedStyle(section, null).getPropertyValue('background-color');
  7.     var timer;
  8.     var interval;
  9.     var currentTime = 0;
  10.     var time = 10000;
  11.     var elapsedTime = 0;
  12.     var childh = section.getElementsByTagName('h2');
  13.     var p;
  14.  
  15.     section.addEventListener('click', function(e) {
  16.         section.style.backgroundColor = 'DarkViolet';
  17.         currentTime = currentTime - elapsedTime + time;
  18.         clearTimeout(timer);
  19.         clearInterval(interval);
  20.         elapsedTime = 0;
  21.         timer = setTimeout(function() {
  22.             if(section.style.backgroundColor === sectionInitialColor) {
  23.                 currentTime = 0;
  24.             } else {
  25.                 currentTime -= time;
  26.             }
  27.             section.style.backgroundColor = sectionInitialColor;   
  28.         }, currentTime);
  29.         interval = setInterval(function() {
  30.             elapsedTime += 1000;
  31.         }, 1000);
  32.     });
  33.  
  34.     for(let i = 0; i < childh.length; i++) {
  35.         childh[i].addEventListener('click', function(e) {
  36.             p = document.createElement('p');
  37.             p.textContent = childh[i].textContent;
  38.             section.appendChild(p);
  39.             e.stopPropagation();
  40.         });
  41.     }
  42.  
  43. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement