我可以在javascript中暂停500毫秒吗?

我可以在javascript中暂停500毫秒吗?,javascript,for-loop,setinterval,Javascript,For Loop,Setinterval,我有这个功能来移动一个物体,我想在我的循环中设置一个500密耳秒的间隔。没有jQuery,只有javascript。我已尝试设置间隔(“”,500) 函数快照(){ document.getElementById('power').play(); 对于(var i=140;i如果您希望每500毫秒运行一次“shot”功能 function shot() { document.getElementById('power').play(); for (var i=140; i<

我有这个功能来移动一个物体,我想在我的循环中设置一个500密耳秒的间隔。没有jQuery,只有javascript。我已尝试设置间隔(“”,500)

函数快照(){
document.getElementById('power').play();
对于(var i=140;i如果您希望每500毫秒运行一次“shot”功能

function shot() {
    document.getElementById('power').play();
    for (var i=140; i<400; i++)
    {  
        document.getElementById('shot').style.visibility='visible';
        imgShot.style.left = parseInt(imgObj.style.left) - 130 + 'px';  
        imgShot.style.top = parseInt(imgObj.style.top) - i + 'px';
    }
}

setInterval(shot,500);
函数快照(){
document.getElementById('power').play();

对于(var i=140;i在JavaScript中阻止顺序执行的唯一方法是创建一个忙循环(或使用一个阻止函数,如
alert
):但您不希望这样做,因为它会冻结用户界面-包括更新元素移动的UI显示

相反,应该对代码进行结构化,使其使用异步回调—此模型允许浏览器事件循环不间断地继续,以便它能够对用户输入做出反应,并根据DOM更改更新显示

document.getElementById('power').play();
document.getElementById('shot').style.visibility='visible';

var i = 140;
function moveIt() {
    imgShot.style.left = parseInt(imgObj.style.left) - 130 + 'px';  
    imgShot.style.top = parseInt(imgObj.style.top) - i + 'px';
    i++;
    if (i < 400) {
      // If have more, schedule again in .5s
      // Remember that setTimeout *returns immediately* because it
      // is an asynchronous operation.
      setTimeout(moveIt, 500);
    } else {
      // This will be executed when animation finishes.
    }
}

moveIt();
// This will be executed after first movement which is well before
// the animation finishes. We could also use setTimeout(moveIt, ..) here,
// depending on what is desired.
document.getElementById('power').play();
document.getElementById('shot').style.visibility='visible';
var i=140;
函数moveIt(){
imgShot.style.left=parseInt(imgObj.style.left)-130+'px';
imgShot.style.top=parseInt(imgObj.style.top)-i+'px';
i++;
如果(i<400){
//如果有更多,请在.5s内再次安排
//记住setTimeout*会立即返回,因为它
//是一个异步操作。
setTimeout(moveIt,500);
}否则{
//这将在动画完成时执行。
}
}
moveIt();
//这将在第一次移动之后执行,而第一次移动早在
//动画结束。我们也可以在这里使用setTimeout(moveIt,…),
//取决于需要什么。
或者,更好的方法是使用类似的方法来处理大量重复的内容(在这种情况下,您可能需要编写一个自定义的缓和,因为运动是从非0初始值沿y轴线性加速的)


setInterval
也可以使用(完成后取消它,而不是启动新的超时),但我发现
setTimeout
方法在概念上更容易显示。区别在于
setInterval
将尝试始终在time=iteration*timeout时运行,在退化的情况下,它可以明显地比多个setTimeout调用更一致,或者,正如Alnitak指出的,它可以有害地进行堆栈/级联。

什么您是否希望
setInterval(''''500);
执行此操作?是否希望在
for
循环中每次迭代后等待500毫秒?我认为您希望
setTimeout
的可能重复项,并逐步递增每次迭代。
setTimeout(func,I*500)
。我只想将循环冻结500毫秒。实际上,
setInterval
超时可能会以堆叠结束,立即触发大量。如果
setTimeout
,则不会发生这种情况。@Alnitak True;这是硬币的另一面。我扩展了最后一句话以确认您的观点。
document.getElementById('power').play();
document.getElementById('shot').style.visibility='visible';

var i = 140;
function moveIt() {
    imgShot.style.left = parseInt(imgObj.style.left) - 130 + 'px';  
    imgShot.style.top = parseInt(imgObj.style.top) - i + 'px';
    i++;
    if (i < 400) {
      // If have more, schedule again in .5s
      // Remember that setTimeout *returns immediately* because it
      // is an asynchronous operation.
      setTimeout(moveIt, 500);
    } else {
      // This will be executed when animation finishes.
    }
}

moveIt();
// This will be executed after first movement which is well before
// the animation finishes. We could also use setTimeout(moveIt, ..) here,
// depending on what is desired.