简体   繁体   English

如何在不使用任何外部API的情况下使javascript函数等待其他函数完成?

[英]How can I make javascript function to wait for other function to finish without using any external APIs?

I have a function that calls other function containing setinterval. 我有一个函数调用包含setinterval的其他函数。 Depending on variables, the second function needs varying amount of time to complete. 根据变量,第二个功能需要不同的时间来完成。 If first function continues before second ends, hell happends. 如果第一个功能在第二个结束之前继续进行,就会发生地狱。

The question is, how can I make first function wait for the second function to complete before proceeding with next line. 问题是,在继续下一行之前,如何使第一个功能等待第二个功能完成。 I'm not allowed to use any external APIs. 我不允许使用任何外部API。 Thanks. 谢谢。

Heres the code but it may be a little hard to understand. 这是代码,但可能有点难以理解。 HTML: HTML:

<html>
<head>
<style>
#canvas { 
background-color: rgba(158, 167, 184, 0.2); }
</style>

</head>
<body onload="room1();">
    <canvas id="canvas" style="border: none;" width="800" height="600"></canvas>    
<script src="main.js"></script> 
</body>
</html>

JS: JS:

var canvas=document.getElementById("canvas");
var gra=canvas.getContext("2d");
gra.font = "20px Courier New";

function pisz(text,x,y, interval) {
x1=x;
var i=0;
var stopPisz=setInterval(function(){ 
if (i<text.length) {
    var audio = new Audio('typwriter.wav');
    audio.volume=.1;
    if (i%3==0 && text[i]!=" ") audio.play();
    gra.fillText(text[i],x1,y);
    x1=x1+12;
    if (x1>700 && text[i]==" "){
        x1=x;
        y=y+22;
        }
    }
    if (i>text.length-2) clearInterval(stopPisz);
    i++;
    },interval); 
}


function room1 () {
text1="Witam cię w mojej grze. Twoim zadaniem jest rozwiązanie zagadek i wydostanie się z labiryntu pokoji.";
text1.split("");
pisz(text1,20,30,50);


text2="gfhfdghd hg fdhfgdfdf fdhfdgdfdf hdgdfhdfgdfghd";
text2.split("");
pisz(text2,20,200,50);

}

I want to achieve something like this: 我想实现以下目标:

function f1 () {

function f2(/*some parameters*/ );
//wait for function f2 to end, then proceed to next line of this function

function f2(/*some parameters*/);
//wait, etc.

}

function f2(/*some parameters*/){
//doing something once
setInterval(function(/*some parameters*/){ 
//repeating something for some time, then clearInterval so the function ends
    },some interval); 
}

Why you didn't use callBacks? 为什么不使用CallBacks?
You can pass any callBack as a function to another function, and call it whenever you want, for example, on http response or after timeOut, or in specific step of interval. 您可以将任何callBack作为函数传递给另一个函数,并可以在任何需要的时候调用它,例如,在http响应上或timeOut之后,或在间隔的特定步骤中。

 var f1 = function(delay, cb) { setInterval(function() { // do whatever you want, and now it's time to call your second function if (cb) cb(); }, delay); } var f2 = function(param) { console.log('second function', param || '---'); } // Use like this: f1(1000, f2); // or : f1(2000, function() { console.log('second function will be call'); f2('with param'); }); 

暂无
暂无

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

相关问题 如何等待外部文件功能完成然后在main上继续? - How can I wait for an external file function to finish then continue on main? 如何使用 javascript 承诺使一个函数等待另一个函数异步完成运行? - How can I use a javascript promise to make one function wait for another to finish running asynchronously? 如何在 javascript 中等待 function 完成? - How to wait for function to finish in javascript? 如何让function等待其他函数完成后再执行 - How to make function wait for other functions to finish before executing 如何在Javascript中阻止功能并等待其他任务完成 - How to block function and wait for other task to finish in Javascript 在执行另一个函数之前,如何使jquery等待一个函数完成? - How can I make jquery wait for one function to finish before executing another function? 我怎样才能让这段代码等待第一个 function 完成,然后再执行第二个或 function,秒数更少? - How can I make this code wait for the first function to finish before executing the second one or the function with less seconds? 如何在Javascript中让等1的方法完成其他 - How to make wait the 1 method to finish other in Javascript 我如何让我的 javascript 函数在继续之前等待 WebSql 事务完成 - How i make my javascript function wait to WebSql transactions to finish before continuing 异步for循环:如何使for循环等待函数完成? - Asynchronous for loop: how do I make the for loop wait for function to finish?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM