gpt4 book ai didi

javascript - Eteration - 解释和示例

转载 作者:行者123 更新时间:2023-12-03 13:25:19 27 4
gpt4 key购买 nike

谁能解释一下 到底是什么?以太并举个例子?

来源:Long running tasks道格拉斯·克罗克福德的 YUI 博客

最佳答案

最初,我以为这只是 的拼写错误。迭代 ,因为在线搜索 eteration 不会产生显着的结果。

但是,然后,我遇到了references声明这个词是克罗克福德自己在他的一次谈话中创造的。
在网上,我唯一能找到解释的地方是他的页面,在 The Factorial Tutorial ,一篇文章,在 第 2 幕 ,作为对代码示例的评论,他指出:

Act 2a: message iteration (eteration)



这似乎是一对相关术语的一部分,因为他的下一个代码示例在不使用堆栈的情况下执行递归,包含该对的另一个成员:

Act 2b: message recursion (ecursion)



所以,似乎 迭代 游览是 Crockford 自己发明和定义的术语,指的是 E Programming Language 上下文中的消息迭代和递归。 , 在 Java 之上为编写分布式应用程序的开发人员设计。

该语言被称为 的事实邮箱 可能是为其特定的迭代和递归风格赋予所选术语(**e***teration* 和 **e***cursion*)的原因。

如果是 Javascript 的上下文,Crockford 解释了术语 迭代 作为谈话的一部分 Crockford on JavaScript -- Scene 6: Loopage , 从分钟 开始30:40 :

Eteration means to break a task into multiple turns so that on each eteration, instead of going through a conventional loop, at the bottom of the loop we call setTimeOut, passing it a function which causes us to do the next eteration. That means that the turns are going to be short — the turn's only as long as one eteration – and we can do as many eterations as we want and not lock up the event loop.



结果是,如果需要太长时间, 会阻塞接口(interface),而不是紧密循环。迭代 将循环的每个步骤安排在一个链中,该链仅在实际步骤执行时阻塞接口(interface),而不是在步骤之间。这使得可以在与接口(interface)相同的线程中执行长时间运行的任务(Javascript 是单线程的),同时保持应用程序的响应能力。

查看质量更高的完整演讲,并附有全文成绩单 here .

此外,有关如何实现这种技术的引用,请考虑以下场景:
<html>
<head>
<script type="text/javascript">
function testFeedback()
{
var feedbackDiv = document.getElementById("feedbackDiv");

feedbackDiv.innerHTML += "The Interface is Still Responsive!</br>";
}

var currentNumber = 0;
var loopStepDelay = 30;

function performLoopStep()
{
var numbersDiv = document.getElementById("numbersDiv");
numbersDiv.innerHTML = currentNumber++;
setTimeout("performLoopStep()", loopStepDelay);
}
setTimeout("performLoopStep()", loopStepDelay);
</script>
</head>
<body>
<div id="numbersDiv"></div>
</br>
</br>
<div id="feedbackDiv"></div>
</br>
</br>
<button onClick="testFeedback()">Try Me</button>
<body>
</html>

有两个 div s,一个显示正在进行的迭代的索引,另一个附加文本界面仍然响应!每按一下 Try Me 按钮。从代码中可以看出,迭代步骤由 setTimeout 安排。相隔一些时间间隔,允许用户交互发生并被处理。因此,当用户单击按钮并触发第二个 div 的更新时,迭代步骤将继续运行,从而保持页面的响应能力,同时完成它必须执行的工作的实际进展(在这种情况下,只是显示索引)。

关于javascript - Eteration - 解释和示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7184896/

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