有没有sleep比使用以下pausecomp函数更好的方法来设计JavaScript (从这里开始)?
function pausecomp(millis)
{
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < millis);
}
Run Code Online (Sandbox Code Playgroud)
这不是JavaScript中Sleep的重复- 动作之间的延迟 ; 我希望在函数中间实现真正的睡眠,而不是在执行代码之前的延迟.
我有一个javascript文件,在几个地方我想添加一个小延迟,所以脚本将达到这一点,等待3秒,然后继续其余的代码.我想到这样做的最好方法是创建一个函数,我可以在脚本的任何地方调用它.
function startDelay(lengthOfDelay)
{
//code to make it delay for lengthOfDelay amount of time
}
Run Code Online (Sandbox Code Playgroud)
但是,我找不到任何方法来实现代码以使其等待.我看了一下setTimeout,但是你需要将函数硬编码到它中,这对我没什么好处.
有什么方法可以让脚本暂停几秒钟吗?在代码暂停时,我对UI冻结没有任何问题.
如果没有,有没有办法可以使用PHP sleep()来实现这一目标?(我知道PHP是服务器端,Javascript是客户端,但也许有一种我没有听说过的方式.)
我需要让代码休眠,直到满足某些条件或超过 3 秒超时。然后返回一个简单的字符串。无论如何我可以做到这一点吗?
// this function needs to return a simple string
function something() {
var conditionOk = false;
var jobWillBeDoneInNMiliseconds = Math.floor(Math.random() * 10000);
setTimeout(function() {
// I need to do something here, but I don't know how long it takes
conditionOk = true;
}, jobWillBeDoneInNMiliseconds);
// I need to stop right here until
// stop here until ( 3000 timeout is passed ) or ( conditionOk == true )
StopHereUntil( conditionOk, 3000 );
return "returned something";
}
Run Code Online (Sandbox Code Playgroud)
这就是我要做的: …
编辑:我在Wordpress方面发布了这个,但有些管理员认为它在这里是因为他们说这是客户端问题,但我不认为这是因为他的脚本工作原样(在非wordpress网站)但只抛出在Wordpress站点上仅在某些浏览器上使用脚本时出现DOM异常11错误.相同的脚本在Wordpress下运行,但仅在Chrome上运行.
当锚链接悬停并将其转换为Wordpress插件时,我已经采用了播放音频剪辑的脚本.
它工作正常,但仅适用于Chrome(Mac).在Firefox或Safari(当前版本)上,脚本会抛出DOM错误.
这是错误:
INVALID_STATE_ERR:DOM异常11:尝试使用不可用或不再可用的对象.
产生错误的行是: html5audio.currentTime=0
以下脚本适用于我在非Wordpress网站上遇到问题的浏览器.http://www.javascriptkit.com/script/script2/soundlink.shtml#current
我已经尝试过研究这个问题并使用一些修复但我无法开始工作.
var html5_audiotypes={
"mp3": "audio/mpeg",
"mp4": "audio/mp4",
"ogg": "audio/ogg",
"wav": "audio/wav"
}
function audio(sound){
var html5audio=document.createElement('audio')
if (html5audio.canPlayType){ //check support for HTML5 audio
for (var i=0; i<arguments.length; i++){
var sourceel=document.createElement('source')
sourceel.setAttribute('src', arguments[i])
if (arguments[i].match(/\.(\w+)$/i))
sourceel.setAttribute('type', html5_audiotypes[RegExp.$1])
html5audio.appendChild(sourceel)
}
html5audio.load()
html5audio.playclip=function(){
html5audio.pause()
html5audio.currentTime=0
html5audio.play()
}
return html5audio
}
}
Run Code Online (Sandbox Code Playgroud) 为什么我们需要将函数传递给Javascript setTimeOut https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.setTimeout
为什么我们不能像简单的那样做
setTimeOut(1000);
Run Code Online (Sandbox Code Playgroud)
我可以在那里传递空或无功能吗?
我想在每次迭代后等待for循环.
javascript ×5
sleep ×3
audio ×1
delay ×1
function ×1
html5 ×1
settimeout ×1
timeout ×1
wait ×1