您现在的位置是:首页 > 电脑 > 

JS函数内部循环动画(Loop animation inside the JS function)

2025-07-26 20:52:30
JS函数内部循环动画(Loop animation inside the JS function) 有一个函数包含一个本地数组和一个以某种方式改变那些数组的循环。 函数也会返回一个数组。 示例如下: function myFunc(expr) { var array1 = [], array2 = []; for (var
JS函数内部循环动画(Loop animation inside the JS function)

有一个函数包含一个本地数组和一个以某种方式改变那些数组的循环。 函数也会返回一个数组。 示例如下:

function myFunc(expr) { var array1 = [], array2 = []; for (var i = 0; i < expr.length; ++i) { // ... } return array2; };

我需要在每个循环步骤后在画布上画一个带DELAY的数组。

我的第一个想法是在循环结束时使用某种“睡眠”功能,但Google告诉我JS没有这种类型的功能。 我不完全理解“setInterval”函数是如何工作的,但据我所知,即使我的外部函数(myFunc)将停止并且由于此外部函数将不会返回“array2”,这将执行。

There is a function that contains a local arrays and a loop which somehow alters those arrays. Also function returns one of arrays. Example below:

function myFunc(expr) { var array1 = [], array2 = []; for (var i = 0; i < expr.length; ++i) { // ... } return array2; };

I need to draw on canvas an array with a DELAY after each loop step.

My first thought was to use a some sort of "sleep" function in the end of the loop, but Google said me that JS has not such type of functi. I not fully understand how works the "setInterval" function but as far as I know this will execute even when my external function (myFunc) will stopped and due to this external function will not returns "array2".

最满意答案

如果我理解正确,你希望循环“暂停”,同时你执行一些功能,在循环内可以使用数据,然后执行一个功能。 当你说“为每个循环步骤”时,我假设它是for循环中的每一步。

function myFunc(expr) { var array1 = [], array2 = []; for (var i = 0; i < expr.length; ++i) { // ... // Perform something here after a certain time, while the loop waits. } return array2; };

据我所知,你无法完全达到这个目的,但是,如果你有可用的数据,你可以“伪造”你想要的行为。

看到这个jsfiddle在哪里我创建了一个例子。

function myFunc(expr) { var array1 = [], array2 = []; for (var i = 0; i < expr.length; ++i) { // ... setTimeout(function(){ // Do whatever you want to do. }, i*500); // i = 0 is instantly, i = 1 is after 0.5s, i=2 is 1s, and so on... } return array2; };

从for循环开始,您的(估计)时间线将如下所示。

0s -> function in timeout when i = 0 is called 0.5s -> function in timeout when i = 1 is called 1.0s -> function in timeout when i = 2 is called

请注意,超时不准确,并且在执行功能之前可能存在时间偏差。

如果修改在函数内部使用的变量,请注意范围问题。

我希望这有帮助!

If I understand correctly, you want the loop to "pause" while you perform some function with data avilable inside the loop and then perform a function. When you say "for each loop step" I assume it's for each step in the for loop.

function myFunc(expr) { var array1 = [], array2 = []; for (var i = 0; i < expr.length; ++i) { // ... // Perform something here after a certain time, while the loop waits. } return array2; };

As far as I know, you won't be able to achieve this exactly, however, if you have the data available you can "fake" the behaviour you want.

See this jsfiddle where I created an example.

function myFunc(expr) { var array1 = [], array2 = []; for (var i = 0; i < expr.length; ++i) { // ... setTimeout(function(){ // Do whatever you want to do. }, i*500); // i = 0 is instantly, i = 1 is after 0.5s, i=2 is 1s, and so on... } return array2; };

Your (estimated) timeline will then be as follows, from when the for loop is started.

0s -> function in timeout when i = 0 is called 0.5s -> function in timeout when i = 1 is called 1.0s -> function in timeout when i = 2 is called

ote that timeout is not precise, and there can be deviati in the time before execution of the function.

Beware of scoping issues if you modify the variables you use inside the function, inside the loop.

I hope this helps!

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/654026.html

相关标签:无
上传时间: 2023-07-28 12:48:13
留言与评论(共有 18 条评论)
本站网友 棒约翰菜单
8分钟前 发表
看到这个jsfiddle在哪里我创建了一个例子
本站网友 林州市肿瘤医院
19分钟前 发表
inside the loop. I hope this helps!
本站网友 100000
4分钟前 发表
i = 1 is after 0.5s
本站网友 丹参滴丸的副作用
25分钟前 发表
and there can be deviati in the time before execution of the function. Beware of scoping issues if you modify the variables you use inside the function
本站网友 内盘外盘是什么意思
29分钟前 发表
and so on... } return array2; }; 从for循环开始
本站网友 丰腴
14分钟前 发表
同时你执行一些功能
本站网友 成人哺乳
5分钟前 发表
array2 = []; for (var i = 0; i < expr.length; ++i) { // ... } return array2; }; 我需要在每个循环步骤后在画布上画一个带DELAY的数组
本站网友 photospeak
7分钟前 发表
同时你执行一些功能
本站网友 北大汇丰商学院
10分钟前 发表
i*500); // i = 0 is instantly
本站网友 煲机教程
22分钟前 发表
i*500); // i = 0 is instantly
本站网友 湖北省建设网
10分钟前 发表
0s -> function in timeout when i = 0 is called 0.5s -> function in timeout when i = 1 is called 1.0s -> function in timeout when i = 2 is called 请注意
本站网友 girlfriend什么意思
7分钟前 发表
看到这个jsfiddle在哪里我创建了一个例子
本站网友 广东省省长
13分钟前 发表
function myFunc(expr) { var array1 = []
本站网友 眼光独到
17分钟前 发表
array2 = []; for (var i = 0; i < expr.length; ++i) { // ... // Perform something here after a certain time
本站网友 怀柔水库
9分钟前 发表
array2 = []; for (var i = 0; i < expr.length; ++i) { // ... // Perform something here after a certain time
本站网友 波动少女2补丁
14分钟前 发表
array2 = []; for (var i = 0; i < expr.length; ++i) { // ... setTimeout(function(){ // Do whatever you want to do. }
本站网友 祛鱼尾纹
25分钟前 发表
但Google告诉我JS没有这种类型的功能