我正在编写一种算法,以给定的速度遍历数组的元素。我用它来遍历我发现的一系列路径中的游戏地图上的单元格。
我需要用新数组调用新函数时,最后一个函数调用停止工作。
这将用于在路径上单击鼠标。现在,如果我们调用第一个函数,它将沿该路径运行,但是如果在该路径的末尾之前,使用新路径再次调用该函数,则它们将继续更改当前坐标。>
对象必须在第一次调用该函数时所到达的位置中断其路径,并在第二个函数调用中继续其路径。
这是它现在如何工作的示例代码:
let coord = {x:0,y:0}
let goByPath = (path=[],coord={})=>{
let i = 0;
pathIteration(i,path,coord)
}
let pathIteration = (i,path,coord)=>{
if(i++<path.length){
setTimeout(()=>{
coord = path[i-1];
console.log(coord);
pathIteration(i,path,coord);
},500);
}
return i;
};
path1 = [{x:0,y:1},{x:1,y:1},{x:1,y:2},{x:2,y:2}];
path2 = [{x:1,y:3},{x:1,y:4},{x:1,y:5}];
goByPath(path1, coord);
setTimeout(()=>{
goByPath(path2, coord);
},900);
立即输出到控制台:
{
"x": 0,
"y": 1
}
{
"x": 1,
"y": 1
}
{
"x": 1,
"y": 3
}
{
"x": 1,
"y": 2
}
{
"x": 1,
"y": 4
}
{
"x": 2,
"y": 2
}
{
"x": 1,
"y": 5
}
所需的输出:
{
"x": 0,
"y": 1
}
{
"x": 1,
"y": 1
}
{
"x": 1,
"y": 3
}
{
"x": 1,
"y": 4
}
{
"x": 1,
"y": 5
}
答案 0 :(得分:4)
您只需要clearTimeout
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearTimeout
let coord = { x: 0, y: 0 };
let myTimeout;
let goByPath = (path = [], coord = {}) => {
let i = 0;
clearTimeout(myTimeout); // stop previous calls
pathIteration(i, path, coord);
}
let pathIteration = (i, path, coord) => {
rect(coord);
if (i++ < path.length) {
myTimeout = setTimeout(() => { // store reference to timeout
coord = path[i - 1];
pathIteration(i, path, coord);
}, 500);
}
return i;
};
/* canvas grid for display purposes */
var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');
var n = 10;
var size = Math.min(innerWidth, innerHeight);
var u = size / n;
var draw = function() {
size = Math.min(innerWidth, innerHeight);
u = size / n;
canvas.width = size;
canvas.height = size;
for (var y = 0; y < n; y++) {
for (var x = 0; x < n; x++) {
ctx.strokeStyle = '#000';
ctx.strokeRect(x * u, y * u, u, u);
}
}
};
draw();
var rect = (coord) => {
ctx.fillStyle = '#888';
ctx.fillRect(coord.x * u, coord.y * u, u, u);
};
/* example */
var path1 = [{x:0,y:1},{x:1,y:1},{x:1,y:2},{x:2,y:2}];
goByPath(path1, coord);
var path2 = [{x:1,y:3},{x:1,y:4},{x:1,y:5}];
setTimeout(() => { goByPath(path2, coord); }, 1600);
body {
margin: 0;
display: grid;
place-content: center;
}
<canvas></canvas>
答案 1 :(得分:0)
您可以有一个简单的多线程实现,如下所示:
let coord = { x: 0, y: 0 }
let coordLock = null
let goByPath = (path = [], coord = {}) => {
const newLock = Math.random()
coordLock = newLock
pathIteration(0, path, coord, newLock)
}
let pathIteration = (i, path, coord, lock) => {
if (lock !== coordLock) return i
if (i++ < path.length) {
setTimeout(() => {
coord = path[i - 1]
console.log(coord)
pathIteration(i, path, coord, lock)
}, 500)
}
return i
}
const path1 = [{ x: 0, y: 1 }, { x: 1, y: 1 }, { x: 1, y: 2 }, { x: 2, y: 2 }]
const path2 = [{ x: 1, y: 3 }, { x: 1, y: 4 }, { x: 1, y: 5 }]
goByPath(path1, coord)
setTimeout(() => goByPath(path2, coord), 900)
话虽这么说,但我认为这种实现方式对这样的任务来说是薄弱的。您应该为此类任务具有适当的面向对象的结构。如果此代码库增加,将来肯定会出现意外结果 要将重要内容更改为坐标,您不能依赖于副作用和全局定义的对象