gpt4 book ai didi

javascript - 停止从 javascript 函数返回,直到 SetTimeout 执行

转载 作者:行者123 更新时间:2023-11-30 21:08:26 30 4
gpt4 key购买 nike

我正在使用 fabric.js 绘制二叉树/图,我想引入延迟以便绘制顺序可见。我在我的代码中使用了 setTimeout 方法,但问题是所有节点都被延迟并同时绘制,这是我不想要的。我想要一个节点,然后一点点延迟然后另一个节点。请帮助我。

代码:

delaytime=5000;

function add(x){
console.log(x);
canvas.add(x);
}



function addRoot(value){

var value=String(value);
var x=new fabric.Circle({ radius: radius,left:width/2,fill:'red',top:radius,originX: 'center', originY: 'center',fill:'red'});
var text= new fabric.Text(value, {fontSize: 10, originX: 'center', originY: 'center',left:width/2,top:radius});

setTimeout(function(){ add(x);},delaytime);
setTimeout(function(){ add(text);},delaytime);



return x;
}

在上面的代码中,两个 setTimeout 调用同时执行,但我希望程序等到它从第一个 setTimeout 调用返回,即使它是 5 秒,然后执行下一个语句。请指点。

Async/await 也不起作用。只有第一次调用被绘制,即 addRoot

测试.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test for fabric</title>

</head>
<body>

<canvas id="canvas" width="800" height="800" style="border:1px solid #000000">


</canvas>
<script src="fabric.js"></script>
<script src="testfunctions.js">

</script>
<script src="./testfunctions.js"></script>
<script>

var canvas = new fabric.Canvas('canvas');
var a10=addRoot(10);
var a20=addNode(a10,20,225);
var a40=addNode(a10,40,315);
var a30=addNode(a20,30,270);

</script>

</body>
</html>

异步代码:

var canvas = new fabric.Canvas('canvas');
var height = parseInt(document.getElementById('canvas').getAttribute('height'));
var width = parseInt(document.getElementById('canvas').getAttribute('width'));
var distance = 200;
var radius = 20;
var linelimit = 180;
var delaytime = 1000;

async function delay (time) {
return new Promise(function (resolve) {
setTimeout(resolve, time)
})
}


async function addRoot (value) {
var value = String(value);
var x = new fabric.Circle({ radius: radius, left: width / 2, fill: 'red', top: radius, originX: 'center', originY: 'center', fill: 'red'});
var text = new fabric.Text(value, {fontSize: 10, originX: 'center', originY: 'center', left: width / 2, top: radius});
await delay(1000);
canvas.add(x);
canvas.add(text);
return x
}

function addline (x1, y1, x2, y2) {
console.log("In addline");
var line = new fabric.Line([x1, y1, x2, y2], {stroke: 'black', originX: 'center', originY: 'center'});
canvas.add(line)
}


async function addNode (node1, node2, angle) {
var intialx = parseInt(node1.left);

var initialy = parseInt(node1.top);

console.log("addNode");

if (angle > 0 && angle <= 90) {
var pointx = Math.abs(Math.abs(Math.cos(angle * Math.PI / 180) * distance) + intialx);
var pointy = Math.abs(Math.abs(Math.sin(angle * Math.PI / 180) * distance) - initialy);

var initiallinex = Math.abs(Math.abs(Math.cos(angle * Math.PI / 180) * radius) + intialx);
var initialliney = Math.abs(Math.abs(Math.sin(angle * Math.PI / 180) * radius) - initialy);
var finallinex = Math.abs(Math.abs(Math.cos(angle * Math.PI / 180) * linelimit) + intialx);
var finalliney = Math.abs(Math.abs(Math.sin(angle * Math.PI / 180) * linelimit) - initialy)
}
if (angle > 90 && angle <= 180) {
var pointx = Math.abs(Math.cos(angle * Math.PI / 180) * distance + intialx);
var pointy = Math.abs(Math.sin(angle * Math.PI / 180) * distance - initialy);

var initiallinex = Math.abs(Math.cos(angle * Math.PI / 180) * radius + intialx);
var initialliney = Math.abs(Math.sin(angle * Math.PI / 180) * radius - initialy);
var finallinex = Math.abs(Math.cos(angle * Math.PI / 180) * linelimit + intialx);
var finalliney = Math.abs(Math.sin(angle * Math.PI / 180) * linelimit - initialy)
}
if (angle > 180 && angle <= 270) {
if (angle == 270) {
var pointx = Math.abs(Math.cos(angle * Math.PI / 180) * distance + intialx);
var pointy = Math.abs(Math.sin(angle * Math.PI / 180) * distance - initialy);
var initiallinex = Math.abs(Math.cos(angle * Math.PI / 180) * radius + intialx);
var initialliney = Math.abs(Math.sin(angle * Math.PI / 180) * radius - initialy);
var finallinex = Math.abs(Math.cos(angle * Math.PI / 180) * linelimit + intialx);
var finalliney = Math.abs(Math.sin(angle * Math.PI / 180) * linelimit - initialy)
} else {
var pointx = Math.abs(Math.cos(angle * Math.PI / 180) * distance + intialx);
var pointy = Math.abs(Math.sin(angle * Math.PI / 180) * distance - initialy);
var initiallinex = Math.abs(Math.cos(angle * Math.PI / 180) * radius + intialx);
var initialliney = Math.abs(Math.sin(angle * Math.PI / 180) * radius - initialy);
var finallinex = Math.abs(Math.cos(angle * Math.PI / 180) * linelimit + intialx);
var finalliney = Math.abs(Math.sin(angle * Math.PI / 180) * linelimit - initialy)
}
} else {
var pointx = Math.abs(Math.cos(angle * Math.PI / 180) * distance + intialx);
var pointy = Math.abs(Math.sin(angle * Math.PI / 180) * distance - initialy);
var initiallinex = Math.abs(Math.cos(angle * Math.PI / 180) * radius + intialx);
var initialliney = Math.abs(Math.sin(angle * Math.PI / 180) * radius - initialy);
var finallinex = Math.abs(Math.cos(angle * Math.PI / 180) * linelimit + intialx);
var finalliney = Math.abs(Math.sin(angle * Math.PI / 180) * linelimit - initialy)
}

var x = new fabric.Circle({ radius: radius, originX: 'center', originY: 'center', fill: 'red', left: pointx, top: pointy});
var value1 = String(node2);
var text = new fabric.Text(value1, {fontSize: 10, originX: 'center', originY: 'center', left: pointx, top: pointy});
await delay(1000);
canvas.add(x);
canvas.add(text);
await delay(1000);
console.log("Executed");
addline(initiallinex, initialliney, finallinex, finalliney);
return x;
}

最佳答案

你可以使用 ES2017 async/await :

function delay (time) {
return new Promise(function (resolve) {
setTimeout(resolve, time)
})
}

let delaytime = 1000

async function addRoot (value) {
console.log('do stuff')
// var value=String(value);
// var x=new fabric.Circle({ radius: radius,left:width/2,fill:'red',top:radius,originX: 'center', originY: 'center',fill:'red'});
// var text= new fabric.Text(value, {fontSize: 10, originX: 'center', originY: 'center',left:width/2,top:radius});
await delay(delaytime)
console.log('add x')
// add(x);
await delay(delaytime)
console.log('add text')
// add(text);
return { value: value }
// return x;
}

// usage
addRoot('some value').then(function (returnedValue) {
console.log(returnedValue)
})

请注意演示中每个 console.log() 输出旁边的时间戳中的延迟。

更新

为了在对您的代码进行最少更改的情况下正确使用此代码,请更改以下代码段:

var canvas = new fabric.Canvas('canvas');
var a10=addRoot(10);
var a20=addNode(a10,20,225);
var a40=addNode(a10,40,315);
var a30=addNode(a20,30,270);

为此:

// to support await operator
(async function () {
// do not declare another canvas, you've already done so in other block
var a10 = await addRoot(10)
var a20 = await addNode(a10, 20, 225)
var a40 = await addNode(a10, 40, 315)
var a30 = await addNode(a20, 30, 270)
})()

关于javascript - 停止从 javascript 函数返回,直到 SetTimeout 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46385292/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com