You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在指定时间后加载Canvas数组?求助Canvas代码延迟执行方案

Solution: Delay Specific Code Lines Execution

Got it, let's get this sorted! You want to run those specific canvas-related lines after a set time—JavaScript's built-in setTimeout() function is exactly what you need here. It lets you schedule code to execute after a specified number of milliseconds.

Modified Code

Here's your updated code with the delay added. I've wrapped the lines you want to delay inside a setTimeout() call (adjust the delay time as needed):

var canvas;
jQuery(document).ready(function(){
 canvas = jQuery('#myCanvas').canvaDots({
 sizeDependConnections: false,
 randomBounceSides: false,
 dotsColor:[0,0,0,0],
 linesColor:[255,255,255],
 mouseReaction: true,
 magnetPowerDelimiter: 5,
 speed: 1.2
 });

 // Delay the following lines to run after 2000ms (2 seconds)
 setTimeout(function() {
    canvas.loadSpecificArray(1, arrayZ);
    canvas.setSpecificArray(1, function(){});
    canvas.setSpecificArrayOffsetX(10);
 }, 2000); // Change this number to adjust delay time (in milliseconds)
});

Key Details to Know

  • How setTimeout() works: The first parameter is a callback function containing the code you want to delay. The second parameter is the delay time in milliseconds (1000ms = 1 second).
  • Adjust the delay: Just change the 2000 value to whatever time you need—for example, 5000 would make the code run after 5 seconds.
  • Individual delays if needed: If you want each line to run at different times, you can create separate setTimeout() calls for each one. Like this:
    // Run first line after 2s, second after 3s, third after 4s
    setTimeout(() => canvas.loadSpecificArray(1, arrayZ), 2000);
    setTimeout(() => canvas.setSpecificArray(1, function(){}), 3000);
    setTimeout(() => canvas.setSpecificArrayOffsetX(10), 4000);
    
  • Scope note: Since canvas is defined in the outer scope of the jQuery ready function, it's accessible inside the setTimeout() callback without any issues.

内容的提问来源于stack exchange,提问作者Mads Hjorth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:19:41