简体   繁体   English

如何编写一个需要X秒钟的函数?

[英]How can I write a function that takes X amount of seconds?

I want to write a javascript function that takes very close to 5 seconds to run. 我想编写一个运行非常接近5秒的javascript函数。 How can I make that guarantee? 我该如何保证?

I have tried 我努力了

  function wait(numSeconds) {
    var end = new Date().getMilliseconds() + numSeconds * 1000;
    while (new Date().getMilliseconds() <= end) {}
  }

but this just crashes the page. 但这只会使页面崩溃。

You cannot freeze the page without freezing the page. 您不能冻结页面而不冻结页面。

You're trying to write a function that runs for 5 seconds straight. 您正在尝试编写一个连续运行5秒的函数。
Javascript runs on the UI thread, so the page will be frozen whenever any code runs. Javascript在UI线程上运行,因此只要运行任何代码,页面就会被冻结。

In short, you can't do that. 简而言之,您无法做到这一点。
You need to write asynchronous code , probably by using setTimeout() . 您可能需要使用setTimeout()来编写异步代码

您可以使用:

var t = setTimeout(function(){alert('done')},5000);

If you want to wait 5 seconds before doing something, use setTimeout or setInterval . 如果要等待5秒钟再做某事,请使用setTimeout或setInterval Otherwise, just eating up user time is a bad idea. 否则,仅仅消耗用户时间是一个坏主意。 In fact, the browsers will interrupt your script if it takes too long to do something, which your function most certainly will do. 实际上,如果执行某件事花费的时间太长,浏览器就会中断您的脚本,而您的功能肯定会这样做。

(Ignoring for the moment why you shouldn't really do this...) (暂时忽略您为什么不应该真正执行此操作...)

The reason your code is freezing the browser is that your while condition will never become true (for numSeconds of 1 or more). 您的代码冻结浏览器的原因是您的while条件将永远不会变为真( numSeconds为1或更大)。

The .getMilliseconds() method returns just the milliseconds part of the date, ie, a number between 0 and 999. For numSeconds of 1 of more your end will always be greater than 999. .getMilliseconds()方法 返回日期的毫秒部分,即0到999之间的一个数字。对于numSeconds (大于1),您的end将始终大于999。

Try using .getTime() instead of .getMilliseconds() . 尝试使用.getTime()而不是.getMilliseconds() Or if you don't care about supporting old browsers uses Date.now() . 或者,如果您不关心支持旧的浏览器,请使用Date.now()

You could just use javascripts setTimeout instead of your own wait function. 您可以只使用javascripts setTimeout代替自己的等待函数。

setTimeout excecutes a function after a certain amount of time. setTimeout在一定时间后执行功能。

For example: 例如:

setTimeout(function(){}, 5000);

Would just wait 5 seconds. 只需等待5秒钟。

See more at w3schools . w3schools中查看更多内容。

Strictly speaking, what you ask cannot be done because, as you've seen, the page will freeze while you wait. 严格来说,您的要求无法完成,因为如您所见,页面将在您等待时冻结。

As a practical matter, though, I suspect that what you really want is the something like this: 但实际上,我怀疑您真正想要的是这样的东西:

function waitawhile() {
    setTimeout( function() {
        alert('5 seconds has passed');
    }, 5000);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写一个可以在几秒钟后撤消操作的函数? - How can I write a function that undoes the action after a few seconds? 如果元素在视口中停留X倍的时间,如何触发函数? - How can I trigger a function if an element is in the viewport for X amount of time? 每&#39;x&#39;秒执行一次.map()函数 - Execute a .map() function every 'x' amount of seconds Javascript:如何创建一个函数,该函数可以从固定数量的类中生成 X 数量的对象? - Javascript: How do I create a function that can generate an X amount of objects drawing from fixed amount of classes? 如何编写一个称为countDots(x)的函数,该函数将字符串作为参数并返回包含的点数(。)。 - How do I write a function called countDots(x) that takes a string as an argument and returns the number of dots (.) it contains 如何创建一个输出类的各种实例的“ x”数量的函数? - How can I create a function that outputs 'x' amount of various instances of a class? 如何每3秒钟调用一次15秒的功能? - How can I call a function every 3 seconds for 15 seconds? 如何编写 function 以延迟 window 加载 3 秒? - How do I write function to delay window load for 3 seconds? 如何编写一个接受值数组并返回对象的函数 - How do I write a function that takes an array of values and returns an object x秒后如何关闭精益模式弹出窗口? - How can I close lean modal popup after x seconds?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM