Guest User

Untitled

a guest
Aug 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. function debounce(func, wait, immediate) {
  2. var timeout;
  3. return function() {
  4. var context = this, args = arguments;
  5. var later = function() {
  6. timeout = null;
  7. if (!immediate) func.apply(context, args);
  8. };
  9. var callNow = immediate && !timeout;
  10. clearTimeout(timeout);
  11. timeout = setTimeout(later, wait);
  12. if (callNow) func.apply(context, args);
  13. };
  14. };
  15.  
  16.  
  17.  
  18.  
  19. function breakAfterFirstWord(){
  20. const h1 = document.querySelector("h1").innerHTML;
  21. const splittedh1 = h1.split(" ");
  22. if(window.innerWidth < 768 && splittedh1.length === 2){
  23. splittedh1.splice(1, 0, '</br>');
  24. document.querySelector("h1").innerHTML = splittedh1.join(' ');
  25. } else if(window.innerWidth > 767 && splittedh1.length === 3){
  26. splittedh1.splice(1, 1);
  27. document.querySelector("h1").innerHTML = splittedh1.join(' ');
  28. }
  29. }
  30.  
  31. var myEfficientFn = debounce(function() {
  32. breakAfterFirstWord();
  33. }, 250);
  34.  
  35. window.addEventListener('resize', myEfficientFn);
  36.  
  37. breakAfterFirstWord()
Add Comment
Please, Sign In to add comment