Converting a recursive function into an asynchronous CPS implementation (javascript)

后端 未结 3 1317
我寻月下人不归
我寻月下人不归 2020-11-29 09:54

Here\'s my function.

    function duplicate_step_through_highlighted (element_jq, target_jq, char_cb) {
        console.log( element_jq);
        var conten         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 10:25

    Here is my solution, it is a more efficient, cleaner and faster way of doing it:

    var start = 0; //Makes sure you start from the very beggining of the paragraph.
    var text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras viverra sem dolor, nec tempor purus luctus vitae. Nulla massa metus, iaculis et orci euismod, faucibus fringilla metus. Sed pellentesque in libero nec.'; //Your text
    var speed = 14; //Of course you can choose your own speed, 0 = instant, the more you add the slower it gets.
    function typeWriter() {
      if (start < text.length) {
        document.querySelector('.demo').innerHTML += text.charAt(start);
        start++;
      }
      setTimeout(typeWriter, speed);
    }
    
    
    

提交回复
热议问题