Javascript 循环中的超时或休眠
下面是我的代码的最简单形式,我在一个数组中循环,并在每次迭代中删除一个随机项,直到循环长度为0。我希望在每次迭代中添加睡眠或延迟,但我不确定在angular1.x或普通js中实现这一点的最佳方法 我已经找到了这个问题,但无法获得最佳答案 有人能帮我吗Javascript 循环中的超时或休眠,javascript,angularjs,timeout,Javascript,Angularjs,Timeout,下面是我的代码的最简单形式,我在一个数组中循环,并在每次迭代中删除一个随机项,直到循环长度为0。我希望在每次迭代中添加睡眠或延迟,但我不确定在angular1.x或普通js中实现这一点的最佳方法 我已经找到了这个问题,但无法获得最佳答案 有人能帮我吗 while($scope.players.length > 0){ random = Math.floor(Math.random() * $scope.players.length); $scope.players.spl
while($scope.players.length > 0){
random = Math.floor(Math.random() * $scope.players.length);
$scope.players.splice(random, 1);
}
你可以为此目的使用
setInterval()方法调用函数或计算表达式
以指定的间隔(以毫秒为单位)
setInterval()方法将继续调用该函数,直到
调用clearInterval()或关闭窗口
setInterval()返回的ID值用作的参数
clearInterval()方法
提示:1000毫秒=1秒
提示:只执行一次函数,在指定数量的
毫秒,使用setTimeout()方法
示例
function _do(){
if($scope.players.length > 0){
random = Math.floor(Math.random() * $scope.players.length);
$scope.players.splice(random, 1);
} else clearInterval(_loop);
}
var _loop = setInterval(_do,1000); //Sleep for 1 sec
你能用吗
var数组=[1,2,3,4,5,6,7]
var区间=null;
函数removeArray(){
random=Math.floor(Math.random()*array.length);
阵列拼接(随机,1);
console.log(数组);
如果(array.length==0){
间隔时间;
}
}
间隔=设置间隔(removeArray,1000)代码>
。作为控制台包装器{
最大高度:100%!重要;
排名:0;
}
编辑->经过一段有用的评论后,我意识到$timeout不是最好的答案。因此,我同意关于使用间隔的其他答案,但我仍然建议使用Angular的内置服务:
(函数(){
"严格使用",;
角度模块('MyApp',[]);
有棱角的
.module('MyApp')
.controller('MyCtrl',MyCtrl');
MyCtrl.$inject=['$scope','$interval'];
函数MyCtrl($scope,$interval){
$scope.players=['a','b','c','d','e'];
var INTERVAL_LENGTH=1000;//这可以是您想要的长度
var INTERVAL_COUNT=$scope.players.length;
如果(间隔计数>0){
$interval(函数(){
var random=Math.floor(Math.random()*$scope.players.length);
$scope.players.splice(随机,1);
},间隔长度,间隔计数)。然后(函数(){
$scope.players='done!';
});
}
}
})();代码>
{{玩家}
您在问题中提到的答案也很有效。请参阅代码片段
var-app=angular.module(“MyApp”,[]).controller(“MyCtrl”,函数($scope){
$scope.players=['a','b','c','d','a','b','c','d','a','b','c','d'];
var ele=angular.element(document.querySelector(“#playersEle”);
html($scope.players);
功能睡眠(ms){
返回新承诺(resolve=>setTimeout(resolve,ms));
}
异步函数demo(){
而($scope.players.length>0){
log('休息一下…');
等待睡眠(2000年);
log(“两秒钟之后”);
random=Math.floor(Math.random()*$scope.players.length);
$scope.players.splice(随机,1);
html($scope.players);
}
}
演示();
});代码>
使用setInterval
功能
这将在一段时间后继续调用该函数。
例如:
在本例中,它将每隔1000微秒调用myFunction
关键:
1000毫秒=1秒
您还可以使用脚本取消setInterval
:
//create an variable for the setInterval
var intervalId = setInterval(myFunction, 2500);
//That is 2.5 seconds
//Now to cancel the interval:
clearInterval(intervalId);
使用clearInterval()代码>以停止间隔。
在()
中输入要停止的间隔名称
调用多个函数或执行某些操作:
要一次调用多个函数或执行其他操作,请使用以下命令:
var intervalId = setInterval(function () {
//put whatever you want here, I'll put some examples
callAFunction();
inputFunction("123Example...");
alert("Hello!");
//Not a function, just as I said, you can do whatever you want here
console.log("setInterval Is great"); //Not a function!!!!!!!!!!!!!
//This is a comment
/*
This is
another
*/
functionCall();
//here is the end of this variable
}, 100); //the number is the amount of ms after it loops again
看到了吗?这是非常有用的
如果我说了你不想做的事,告诉我
^_^您的答案中没有while循环,也没有退出策略。我在原始问题中指的是while循环,我将在代码中添加它以便于澄清。我只是不认为它会暂停while循环的执行:p
var intervalId = setInterval(function () {
//put whatever you want here, I'll put some examples
callAFunction();
inputFunction("123Example...");
alert("Hello!");
//Not a function, just as I said, you can do whatever you want here
console.log("setInterval Is great"); //Not a function!!!!!!!!!!!!!
//This is a comment
/*
This is
another
*/
functionCall();
//here is the end of this variable
}, 100); //the number is the amount of ms after it loops again