Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Start printing text with a delay of n seconds.

Masha

New Coder
Hello world. I have a script that runs
Printing text with a delay, but through checkbox inclusions. I need to
Printing of the text began after some time
after the entire page loads. Knowledge of JS is almost zero.
Script code
Code:
"use strict";
 
window.onload = function () {
    var i = 2;
    var timer = setInterval(function () {
        i--;
        document.getElementById('timer') + i;
        if (i == 0) {
            document.getElementById('example').style.display = 'block';
            document.getElementById('timer').style.display = 'none';
            clearInterval(timer);
        }
    }, 1000)
}


        var textNodes = [];
        var textNodes_data = [];
        var i = 0;
        var d = document.getElementById('test');
 
        search_textNodes(d);
 
        document.querySelector('.envelope__check').onchange = function() {
            if(this.checked) setTimeout(str_write, 2000);//время задержки печати
        };
        
        function search_textNodes(b) {
            b = b.childNodes;
            for (var c = 0, d = b.length; c < d; c++) {
                var a = b[c];
                if (a.nodeType == 3) {
                    textNodes.push(a);
                    textNodes_data.push(a.data);
                    a.data = "";
                } else {
                    if (a.hasChildNodes()) search_textNodes(a);
                }
            }
        }
        
        function str_write() {
            var a = textNodes_data[i];
если (a) {
                textNodes[i].data += a.charAt(0);
                textNodes_data[i] = a.substr(1);
            } else i++;
            if (i < textNodes.length) setTimeout(str_write, 90);
        
        }

[/КОД]
 

Attachments

  • Для форума.zip
    466.8 KB · Views: 0
Hey there, could you explain a bit more what you're trying to do, or what problem you're having?

Are you trying to add a delay of a few seconds before printing the text?
 
The script I presented works as follows:
- opens the page
-after 1 second, a checkbox appears
- I tick the checkbox
-After 2 seconds, the text starts typing.
I want to
  • so that the page loads in full
  • 2 seconds after that, the text is printed (the checkbox is not used in this case).
 
Thank you all. Here's the solution. Delays of 5 seconds.
Code:
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Привет</title>
<style type="text/css">
html, body {
    background: url(1.jpg) no-repeat center center fixed;
}
#test {
    width: 30vw;
    height: 23vw;
    position: relative;;
    left: -18em;
    bottom: -3em;
    right: 0;
    margin: auto;
    text-align: center;
    font-size: 20px;
    font-family: garamond serif;
    font-weight: bold;
    font-style: italic;
    color: red;
    font: 600 30px/1.3 'Syncopate', sans-serif;
    text-shadow: 0 1px 0 #ccc,
    0 2px 0 #c9c9c9,
    0 3px 0 #bbb,
    0 4px 0 #b9b9b9,
    0 5px 0 #aaa,
    0 6px 1px   rgba(0,0,0, .10),
    0 0 5px   rgba(0,0,0, .10),
    0 1px 3px   rgba(0,0,0, .30),
    0 3px 5px   rgba(0,0,0, .20),
    0 5px 10px  rgba(0,0,0, .25),
    0 10px 10px rgba(0,0,0, .20),
    0 20px 20px rgba(0,0,0, .15);
    text-transform: uppercase;
    font-size: 1.2vw;
    opacity: 0;
}
#test.show {
    opacity: 1;
}
</style>
  </head>
  <body>
<div id="test" >
Белеет парус одинокий<br>
В тумане моря голубом!..<br>
Что ищет он в стране далекой?<br>
Что кинул он в краю родном?...<br><br>
Играют волны — ветер свищет,<br>
И мачта гнется и скрипит...<br>
Увы! Он счастия не ищет<br>
И не от счастия бежит!
</center></div>
<script>
function pechat() {
  var textNodes =[];
  var textNodes_data =[];
  var i = 0;
  function search_textNodes(b) {
    b = b.childNodes;
    for (var c = 0, d = b.length; c < d; c++) {
      var a = b[c];
      if (a.nodeType == 3) {
        textNodes.push(a);
        textNodes_data.push(a.data);
        a.data = ""
      } else a.hasChildNodes() && search_textNodes(a)
    }
  };
  var d = document.getElementById('test');
  search_textNodes(d);
  function str_write() {
    var a = textNodes_data[i];
    if (a) {
      textNodes[i].data += a.charAt(0);
      textNodes_data[i] = a.substr(1)
    } else i++;
    i < textNodes.length && setTimeout(str_write, 60)
  };
  d.classList.add('show');
  str_write()
}
setTimeout(pechat, 2000);
</script>
</body>
</html>
 

Attachments

  • Печатаем текст с задержкой n секунд.zip
    465.7 KB · Views: 0

New Threads

Buy us a coffee!

Back
Top Bottom