如何延迟功能或如何在平局中使用settimout

时间:2019-01-25 12:11:32

标签: javascript

我正在尝试延迟图形元素的开始。因为我已经在draw中的setTimeout中调用了该函数,但是无论我将其设置为多高都没有延迟。还有其他可能或特定原因导致它无法正常工作吗?

var expension = 100;

function draw() {
  ctx.clearRect(0,0, width, height);
  PerlinCircle('1', centerX , centerY, expension, 5, 0, 'rgba(0,0,0,'+alpha+')', true);//this is supposed to start right away
  setTimeout(PerlinCircle('1', centerX , centerY, expension, 5, 0, 'rgba(0,0,0,'+alpha+')', true),3000);//this is supposed to be delayed 
}

//following functions are only added for explanation

function resize() {
  width = canvas.width = window.innerWidth * 2;
  height = canvas.height = window.innerHeight * 2;
  centerX = width / 2;
  centerY = height / 2;
  canvas.style.width = centerX + 'px';
  canvas.style.height = centerY + 'px';
}

function animate() {
  requestAnimationFrame(animate);
  now = Date.now();
  elapsed = now - then;
  if (elapsed > fpsInterval) {
    then = now - (elapsed % fpsInterval);
    draw()
    expension += expensionVelocity;
    alpha=1.5-(expension)/maxExpension;
    console.log(alpha);
   if (expension>=maxExpension+(2*maxExpension/3)){
expension=minExpension;

   }
}
}


function PerlinCircle(id, X, Y, minSize, maxSize, lineWidth, FG, fill) {
  ctx.translate(X, Y);
  ctx.strokeStyle = FG;
  ctx.lineWidth = lineWidth;
  ctx.lineCap = 'round';

  if(fill) ctx.fillStyle = FG;

  let rand = savedRandom[id]?
      savedRandom[id] : savedRandom[id] = Math.random();
  let diff = findNextCoords(0, minSize, maxSize, rand);
  let dx = diff.x, dy = diff.y;
  let i = 0;
  let px = dx, py = dy;

  ctx.beginPath();

  while (i != SEGMENTS) {
    i += 1;

    diff = findNextCoords(i, minSize, maxSize, rand);
    dx = diff.x;
    dy = diff.y;

    ctx.lineTo(px, py);

    if(!matrix[i]) {
      matrix.push(px, py);
    }

ctx.quadraticCurveTo(dx, dy, (px+dx)/2, (py+dy)/2);

    px = dx;
    py = dy;
  }

  ctx.closePath();
  ctx.stroke();

  if(fill) ctx.fill();

  ctx.translate(-X, -Y);
}

我希望使setTimeout函数延迟设置时间的开始。但是根本不拖延。

1 个答案:

答案 0 :(得分:0)

您正在timout内部使用函数调用。请在下面尝试此代码。

HttpListenerTImeoutManager