Advertisement
Guest User

Untitled

a guest
Aug 31st, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <div id="content"></p>
  6.  
  7. <script>
  8. // Phrase a ecrire
  9. var sentence = "Ouh, la grosse boulette.";
  10. // Nous avons dans une phrase "N caracteres + 1" (il faut compter le <br> comme un caractere unique).
  11. var sentence_length = sentence.length + 1;
  12.  
  13. // Nombre de ligne a ecrire.
  14. var line_number = 200;
  15.  
  16. // Ecrire le caractere toutes les X milliseconds.
  17. var print_char_every_ms =  1 * 10;
  18.  
  19.  
  20. // Cette fonction sert a afficher un seul et unique caractere de la phrase de base.
  21. function print_char(index) {
  22.     // L'index est egale au Nieme caractere que nous voulons print.
  23.     // ligne 1
  24.     // O = 0
  25.     // u = 1
  26.     // h = 2
  27.     // ...
  28.     // e = 19
  29.     // t = 20
  30.     // t = 21
  31.     // e = 22
  32.     // . = 23
  33.     // <br> = 24
  34.  
  35.     // ligne 2
  36.     // O = 25
  37.     // u = 26
  38.     // h = 27
  39.     // ...
  40.     // e = 43
  41.     // t = 44
  42.     // t = 45
  43.     // e = 46
  44.     // . = 47
  45.     // <br> = 48
  46.  
  47.     setTimeout(function()
  48.     {
  49.         var content = document.getElementById("content");
  50.         // Postion dans la phrase d'origine (math).
  51.         var position = index % sentence_length;
  52.  
  53.         // Si la position est egale au dernier caractere de la phrase, on affiche le <br>.
  54.         if (position === sentence_length - 1)
  55.         {
  56.             content.innerHTML += "<br>";
  57.         }
  58.         else // Sinon, on affiche le caractere a la position de la phrase d'origine.
  59.         {
  60.             content.innerHTML += sentence[position];
  61.         }
  62.     }, index * print_char_every_ms) // on affiche le caractere voulu toutes les X milliseconds.
  63.                                     // Index va de 0 a "line_number * sentence_length"
  64.                                     // print_char_every_ms = 10ms
  65.                                     // ligne 1
  66.                                     // O = 0 * 10ms
  67.                                     // u = 1 * 10ms
  68.                                     // h = 2 * 10ms
  69.                                     // ...
  70.                                     // ligne 2
  71.                                     // O = 25 * 10ms
  72.                                     // u = 26 * 10ms
  73.                                     // h = 27 * 10ms
  74.                                     // ...
  75.  
  76.  
  77. }
  78.  
  79.  
  80. // Pourquoi faire ca plutot que d'appeler le setTimeout directement ici
  81. // https://coderwall.com/p/_ppzrw/be-careful-with-settimeout-in-loops
  82. for (var index = 0; index < (line_number * (sentence_length)) - 1; ++index)
  83. {
  84.     print_char(index);
  85. }
  86.  
  87. </script>
  88.  
  89. </body>
  90. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement