为什么会出现在JavaScript中没有睡觉的功能时,有setTimeout和setInterval的? [英] Why there is no sleep functionality in javascript when there is setTimeout and setInterval?

查看:108
本文介绍了为什么会出现在JavaScript中没有睡觉的功能时,有setTimeout和setInterval的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么没有在JavaScript中没有这样的函数,它的设置一个超时的延续,保存必要的状态(范围对象和执行点),终止脚本,并给出了控制返回给浏览器?后超时,浏览器将装载回的执行上下文,并继续该脚本,我们将有一个真正的非浏览器阻止睡眠的功能,将工作,即使JS引擎是单线程的。

Why there no such function in javascript that sets a timeout for its continuation, saves the necessary state (the scope object and the execution point), terminates the script and gives the control back to the browser? After the timeout expires the browser would load back the execution context and continues the script, and we would have a real non browser blocking sleep functionality that would work even if the JS engine is single threaded.

为什么仍然有在JavaScript中没有这样的功能?为什么我们仍然切片我们code到功能和超时设定为下一步实现睡眠的效果呢?

Why there is still no such functionality in javascript? Why do we have to still slice our code into functions and set the timeouts to the next step to achieve the sleep effect?

推荐答案

我觉得'sleep'ing是你不希望在您的浏览器。

I think 'sleep'ing is something you do not want in your browser.

首先它可能还不清楚有发生,当你真正睡觉的浏览器应该如何表现。

First of all it might be not clear what has to happen and how a browser should behave when you actually sleep.


  • 是完整的脚本运行时睡着了吗?通常它应该,因为你只能有一个线程运行你的code。如果睡眠过程中的其他事件oocur会发生什么?他们将阻止,并尽快继续执行所有阻止的事件会触发。这将导致一个奇怪的行为,如你所想(为此被解雇了一段时间,也许秒,实际点击后,例如鼠标点击事件)。或这些事件必须被忽略,这将导致信息丢失。

  • Is the complete Script runtime sleeping? Normally it should because you only have one thread running your code. So what happens if other events oocur during sleep? they would block, and as soon execution continues all blocked events would fire. That will cause an odd behaviour as you might imagine (for instance mouse click events which are fired some time, maybe seconds, after the actual click). Or these events had to be ignored, which will lead to a loss of information.

会发生什么浏览器?要把它等待睡眠,如果用户点击一个(例如关闭窗口)按钮?我想不会,但这实际上可能再次调用JavaScript code(卸载),这将不能够因为程序执行睡觉被调用。

What will happen to your browser? Shall it wait for sleep if the user clicks a (e.g. close window) button? I think not, but this might actually call javascript code again (unload) which will not be able to be called since program execution is sleeping.

在第二个想法睡眠是程序设计不良的标志。其实程序/功能/你的名字都有一定的任务,应尽快完成。有时候,你不得不等待结果(比如你等待XHR完成),并要同时继续执行程序。在这种情况下,你可以而且应该使用异步调用。这导致两个优点:

On a second thought sleep is a sign of poor program design. Actually a program/function/you name it has a certain task, which shall be completed as soon as possible. Sometimes you have to wait for a result (for instance you wait for an XHR to complete) and you want to continue program execution meanwhile. In this case you can and should use asynchronous calls. This results in two advantages:


  • 所有脚本的速度提高了(没有其他脚本由于睡眠的拦截)

  • 的code是什么时候它应该执行的,而不是之前还是有一定的事件发生后(如果两个功能检查相同的条件下可能导致死锁像其他问题...)

...这导致了另一个问题:设想$ C $的c两片或多片称之为睡眠。如果他们试图在同一睡觉,也许他们不必要的会阻碍自己。这将导致很多麻烦,当你要调试,也许你甚至有困难,在确保其功能首先睡觉,因为你可能在某种程度上控制这种行为。

... which leads to another problem: Imagine two or more pieces of code would call sleep. They would hinder themselves if they try to sleep at the same, maybe unnecessarily. This would cause a lot of trouble when you like to debug, maybe you even have difficulties in ensuring which function sleeps first, because you might control this behavior somehow.

嗯,我认为这是一段JavaScript的好的部分之一,睡眠不存在。然而,它可能是有趣的JavaScript线程如何可以在浏览器中执行;)

Well I think that it is one of the good parts of Javascript, that sleep does not exist. However it might be interesting how multithreaded javascripts could perform in a browser ;)

这篇关于为什么会出现在JavaScript中没有睡觉的功能时,有setTimeout和setInterval的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆