简体   繁体   English

Javascript使函数一个接一个地执行

[英]Javascript make functions execute one after another

I've got some functions all set up. 我已经设置了一些功能。 One's a typing effect, one is for paragraphs, other for adding " " and one for a blinking cursor like in the terminal. 一个是打字效果,一个用于段落,另一个用于添加“”,一个用于闪烁光标,如终端。 All these change the innerHTML of a div. 所有这些都改变了div的innerHTML。

When the page loads the function Start() has about 10 functions inside it that makes up a sequence of what should happen: Text is written, cursor starts blinking, paragraph, text written again, etc, etc. 当页面加载函数时,Start()里面有大约10个函数组成了一系列应该发生的事情:写入文本,光标开始闪烁,段落,再次写入文本等等。

The thing is, they all execute at the same time unless I use setTimeout() for each one inside the Start() function. 问题是,除非我对Start()函数中的每一个使用setTimeout(),否则它们都会同时执行。 And that's kind of messed up given that I have to define a start time for each one of the functions. 鉴于我必须为每个函数定义一个开始时间,这就搞砸了。

EDIT: No JQuery here. 编辑:这里没有JQuery。 Just javascript. 只是javascript。 This is my whole JS file: 这是我的整个JS文件:

ctrl = 0;
y=0;
block = 0;
test = "";
first_time = 1;

function typing(id, sentence){

        var index=0;
        var intObject= setInterval(function() {
               document.getElementById(id).innerHTML+=sentence[index]; 
               index++;
               if(index==sentence.length){
                     clearInterval(intObject);
                }
            }, 100);
}


function paragraph(x){
    while(x>0){
        document.getElementById("container").innerHTML+="<br>";
        x--;
    }
}

function advance(x){
    while(x>0){
        document.getElementById("container").innerHTML+="&nbsp;";
        x--;
    }
}


function blink(y){
    if(first_time == 1){ctrl = y; first_time=0;}
    if(ctrl!=0){
        if(block=='0'){
        test = document.getElementById("container").innerHTML;
            document.getElementById("container").innerHTML+="\u258B";
            block=1;
        }
        else if(block=='1'){
            document.getElementById("container").innerHTML=test;
            block=0;
        }
        ctrl--;
        setTimeout("blink(y);", 300);
    }
    if(ctrl==0){first_time=1;}
}


function start(){
    typing('container','Subject Name:');
    setTimeout("blink('4');",1700);
    setTimeout("typing('container',' Carlos Miguel Fernando');",2800);
    setTimeout("blink('6');",5600);
    setTimeout("paragraph('1');",7200);
    setTimeout("blink('8');",7400);
    setTimeout("typing('container','Age: 21');",9500);
    setTimeout("blink('4');",10800);
    setTimeout("paragraph('1');",12800);
    setTimeout("blink('4');",13200);
    setTimeout("typing('container','Location: Povoa de Varzim, Portugal');",14500);
    setTimeout("blink('14');",19000);
    setTimeout(function(){document.getElementById("more").style.display="block";}, 23000);
    setTimeout("typing('more','Find Out More');",24000);
}

First you need a way to find out when a function has ended. 首先,您需要一种方法来找出函数何时结束。 The ideal mechanism is called a promise. 理想的机制称为承诺。 There's a good implementation in jQuery. 在jQuery中有一个很好的实现。 Suppose in your list of activities you wanted an artificial delay: 假设您的活动列表中存在人为延迟:

blink(4);
sleep(1000);   // wait 1 second
blink(4);

Implement that like this: 像这样实现:

var sleep = function(ms) {
    var result = $.Deferred();
    setTimeout(result.resolve, ms);
    return result.promise();
};

ie create a $.Deferred , and return its promise() , but in between, start some activity that will complete at a later time. 即创建$.Deferred ,并返回其promise() ,但在两者之间,启动一些将在稍后完成的活动。 When it does, call resolve() (here I just get setTimeout to call it directly). 当它这样做时,调用resolve() (这里我只是让setTimeout直接调用它)。 You can pass a value to resolve to be the logical "return value" of your function. 您可以传递一个值来resolve为您函数的逻辑“返回值”。 Also you can instead call reject which is logically like throwing an exception. 您也可以调用reject ,这在逻辑上就像抛出异常一样。

Once you have a set of building block functions that return promises you can do this: 一旦你有一组返回promises的构建块函数,你可以这样做:

typing('container','Subject Name:').then(function() {
   return blink('4');
}).then(function() {
   return typing('container',' Test');
}).then(function() {
   return blink('4');
}).then(function() {
   // and so on
});

UPDATE: 更新:

Click here to see a quick mock-up in jsbin . 点击这里查看jsbin中的快速模型

check below code 检查以下代码

function blink(id)
{
    //Do something
    alert(id);

    if(id == 2)
    {
        //call typing with text
        blink(4);
    }
    if(id == 4)
    {
        //call typing with text
        blink(6);
    }
    if(id == 6)
    {
        //call typing with text
        blink(8);
    }
    if(id == 8)
    {
        //Complete
    }
}

And call blink(2); 并打电话给眨眼(2); it will call blink function one after another. 它会一个接一个地调用闪烁功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM