JavaScript simulates keyboard typing

  • 2020-06-22 23:47:54
  • OfStack


$(function () {
 
 
      var input_type = {
        init:function ($obj) {
          this.name = $obj.html().split("")
          this.length = this.name.length;
          this.i = 0;
        },
        pri:function () {
          var $this = this
          // You can only use closures here because windown.settimeout Make the function of the this Point to the object windown Not the prototype chain this Object. And now I have to recurse, so I just have to this Object is passed into the closure, recursive anonymous closure function. 
          return (function () {
            if ($this.i > $this.length) {
              window.clearTimeout(Go)
              return false;
            }
            var char = $this.name
            $(".div1").append(char[$this.i])
            $this.i++
            var Go = window.setTimeout(arguments.callee, 100)// Here, arguments.callee Because it is an anonymous closure, the function itself is called. 
          })
        }
      }
 
 
// To establish class class 
      function Input_type() {
        this.init.apply(this, arguments)
      }
 
      Input_type.prototype = input_type
 
// Create an instance 
      var p = new Input_type($(".content"))
      p.pri()()
 
    });

Conclusion: In order to realize the interval time of each cycle, window.settimeout recursion is used. Because you want to encapsulate the this conflict with the stereotype chain, you recursively call the anonymous closure function. arguments.callee is used for anonymous functions.

HTML code:


<button id="pri">pri</button>
<div class="content" style="display: none;">
   I don't see my father anymore 2 More than years, I can not forget his back. That winter my grandmother died and my father lost his job. Misfortune never comes singly. I went from Beijing to Xuzhou to attend my father's funeral and go home. When I saw my father in Xuzhou, the sight of the mess in the courtyard and the memory of my grandmother made me burst into tears. "Said the father." There is no need to feel sad now that things have come to such a pass. Fortunately, heaven never shuts one door but another!" 
   When he came home, he sold and pledged, and his father paid the debt; And borrowed money for the funeral. These days, the family is in dire straits. 1 Partly for the funeral, 1 Partly because of my father's unemployment. After the funeral, father was to go to Nanjing to look for work, and I was to return to Beijing to study, so we went together. 
   When I arrived in Nanjing, I was invited to hang out by some friends 1 Day; The first 2 I had to cross the river to Pukou in the morning and take the bus north in the afternoon. My father had agreed not to see me off because he was busy, and had called me to the hotel 1 A familiar waiter accompanied me. He again 3 Tell the steward to be very careful. But at last he was not at ease, for fear that the steward would not behave properly. A pause 1 Will be. In fact, I was that year 210 I have been to Beijing for two years 3 Then there is nothing that matters. He hesitated for 1 Yes, I finally decided to send me by myself. I'm two 3 To persuade him not to go; He only said, "Never mind, it's not good for them to go!" 
   We crossed the river and entered the station. While I bought the ticket, he was busy looking after the luggage. There's too much luggage. You'll have to pay the porter a small fee to get there. He was busy bargaining with them again. I was too clever at the time. I always thought he was not very pretty and had to interrupt myself. But he finally fixed the price; Just give me a ride. He picked me the one near the door 1 Chair; I spread on the seat the brownish fur-lined overcoat he had made for me. He told me to be careful on the way and to be vigilant at night not to catch cold. He asked the steward to take good care of me. I laughed in my heart at his devilment; They know nothing but money, and they are entrusted with nothing! Besides, can't a man of my age take care of himself? Oh, when I come to think of it now, how clever I was! 
   I said, "Dad, you can go." He looked out of the window and said, "I'm going to buy you some oranges. Just stay here. Don't move about." I saw several vendors waiting for customers outside the railings of the platform. To get to that platform, you had to cross the tracks, jump down and climb up. His father is a 1 Fat man, it would be difficult to walk there. I would have gone, but he refused, so I had to let him go. I saw him in a black cloth cap, black cloth mandarin jacket and dark blue cotton-padded cloth robe hobbling to the railway track and leaning out slowly. But when he crossed the railway track, he had a hard time climbing to the other platform. He held it with his hands, and his feet went up; His fat body tipped slightly to the left, showing an effort. When I saw his back, tears streamed down my face. I quickly wiped away my tears lest he or others should see me. When I looked out again, he was already holding the vermilion oranges and looking back. When crossing the railway, he first scattered the oranges on the ground, climbed down slowly, and then picked them up. When he reached it, I hastened to help him by the hand. He and I walked to the car and took the orange 1 The femoral head rested on my fur coat. So patting clothes on the soil, the heart is very relaxed, like 1 Will say, "I'm leaving; Write from over there!" I watched him go out. After a few steps, he looked back at me and said, "Go in. Nobody's there." And when his figure was lost among the people coming and going, and I could no longer find it, I came in and sat down, and my tears came again. 
   In recent years, my father and I have been living in an unsettled state 1 Day is better than 1 Day. He went out to make a living as a young man, supported himself and did many great things. That knows the old age but so decadent! His eyes were sore, and he could not help himself. Emotion in the yu, naturally to hair in the outside; Domestic trivialities often exasperate him. He treated me differently. But in the last two years, he finally forgot my bad, just thinking about me and my son. When I came north, he wrote 1 Give me the letter. It says, "I am all right except for a severe pain in my arm. I even have trouble using chopsticks or writing brushes. When I read this, I saw the back of the fat man in the green cotton gown and the black cloth mandarin jacket through the glistening tears. Alas! I wonder when I shall see him again! 
</div>
<div class="div1">

</div>


Related articles: