setTimeout的递归方法

时间:2018-06-26 09:00:02

标签: javascript jquery settimeout

我正在尝试使用一种方法创建一个js对象,该方法将逐个字母地打印字母,每个字母之间的延迟为5秒。但是现在没有运行。它立即写入。我的错误在哪里?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <div id="body-window">

</div>
Mat img = imread("image.jpg", IMREAD_GRAYSCALE);
Mat outImg = img.clone();

int step = img.cols; //pitch

IppiSize size;
size.width = img.cols; // size of IPP images is the same
size.height = img.rows; // as read image img

vector<Ipp8u> aBuffer(1);

ippiFilterSharpenBorder_8u_C1R((const Ipp8u*)&img.data[0], step, (Ipp8u*)&outImg.data[0], step, size, ippMskSize5x5, ippBorderConst, 0, aBuffer.data());

imshow("Out", outImg);

3 个答案:

答案 0 :(得分:5)

使用

setTimeout(this.startTyping(index+1),5000);

您正在将startTyping的结果作为要通过setTimeout调用的函数(即undefined)进行传递,并且也不会延迟地立即调用它。尝试:

var that = this;
setTimeout(function() {
  that.startTyping(index+1);
}, 5000);

答案 1 :(得分:3)

执行此操作:

 class autoWrite{
        constructor(id,text){
            this.id = id;
            this.text = text;
        }
        startTyping(index=0){
            if(index==this.text.length){
                console.log("finish");
            }
            else{
                $("#"+this.id).append(this.text.charAt(index));
                console.log(this.text.charAt(index))
                setTimeout(() => this.startTyping(index+1), 500);
            }
        }
    }
    
    
    a = new autoWrite("body-window","hello world");
    a.startTyping();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <div id="body-window">

</div>

或者,您可以像这样使用setInterval()(更好的方法):

 class autoWrite{
        constructor(id,text){
            this.id = id;
            this.text = text;
            this.index = 0;
        }
        startTyping(){
            if(this.index==this.text.length){
                console.log("finish");
                this.index += 1;
            } else {  $("#"+this.id).append(this.text.charAt(this.index));
                console.log(this.text.charAt(this.index));
                this.index += 1;
            }
        }
        start(){
           setInterval(() => {
             if(this.index <= this.text.length){
               this.startTyping();
             }
           }, 500);
          
        }
    }
    
    
    a = new autoWrite("body-window","hello world");
    a.start();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <div id="body-window">

</div>

答案 2 :(得分:1)

如MDN here的setTimeout文档中所述,第一个参数是对延迟后需要执行的函数的引用。

您可以使用以下代码。

class autoWrite{
    constructor(id,text){
        this.id = id;
        this.text = text;
    }
    startTyping(index=0){
        if(index==this.text.length){
            console.log("finish");
        }
        else{
            $("#"+this.id).append(this.text.charAt(index));
			let obj = this;
            setTimeout(function () {
				obj.startTyping(index+1);
            },5000);
        }
    }
}
 a = new autoWrite("body-window","hello world");
 a.startTyping();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <div id="body-window">

</div>