延迟后如何在数组中显示一组项目?
How to display set of items in array after after some delay?
我正在开发一个聊天应用程序。
我立即从 api 获取所有聊天记录并将其存储在数组名称 $scope.channel 中
我正在使用 ng-repeat 显示数组中存在的这些项目列表。我想要做的是在某些时间间隔内显示数组中的数据集。例如,当用户打开聊天屏幕时 he/she 应该能够看到几个 chats/images。然后几秒钟后将加载更多数据。
正如@Sajjad Shahi 建议的那样,您可以使用 $timeout
服务。也许像下面这样。您可以设置所需的延迟和卡盘尺寸。使用您的 $scope.channel
数组作为 srcArray
,使用空数组 yourArrayToDisplay
作为 destArray
。在 ng-repeat
指令中将 channel
替换为 yourArrayToDisplay
。据我所知,这应该会给你想要的结果。
var dalayedChunkHandler = {
chunkSize: 1,
delay: 2000,
timeout: null,
srcArray: [],
destArray: [],
dalayedChunk: function(){
var _this = this;
var len = _this.destArray.length;
Array.prototype.push.apply(_this.destArray,_this.srcArray.slice(len, len + _this.chunkSize));
if(_this.srcArray.length > _this.destArray.length) _this.start();
},
start: function(){
var _this = this;
_this.timeout = $timeout(function(){_this.dalayedChunk.call(_this)}, _this.delay)
}
};
$scope.yourArrayToDisplay = []; // use this array for ng-repeat
dalayedChunkHandler.destArray = $scope.yourArrayToDisplay;
dalayedChunkHandler.srcArray = $scope.chatHistoryArray; // the array you got
dalayedChunkHandler.start();
我正在开发一个聊天应用程序。 我立即从 api 获取所有聊天记录并将其存储在数组名称 $scope.channel 中 我正在使用 ng-repeat 显示数组中存在的这些项目列表。我想要做的是在某些时间间隔内显示数组中的数据集。例如,当用户打开聊天屏幕时 he/she 应该能够看到几个 chats/images。然后几秒钟后将加载更多数据。
正如@Sajjad Shahi 建议的那样,您可以使用 $timeout
服务。也许像下面这样。您可以设置所需的延迟和卡盘尺寸。使用您的 $scope.channel
数组作为 srcArray
,使用空数组 yourArrayToDisplay
作为 destArray
。在 ng-repeat
指令中将 channel
替换为 yourArrayToDisplay
。据我所知,这应该会给你想要的结果。
var dalayedChunkHandler = {
chunkSize: 1,
delay: 2000,
timeout: null,
srcArray: [],
destArray: [],
dalayedChunk: function(){
var _this = this;
var len = _this.destArray.length;
Array.prototype.push.apply(_this.destArray,_this.srcArray.slice(len, len + _this.chunkSize));
if(_this.srcArray.length > _this.destArray.length) _this.start();
},
start: function(){
var _this = this;
_this.timeout = $timeout(function(){_this.dalayedChunk.call(_this)}, _this.delay)
}
};
$scope.yourArrayToDisplay = []; // use this array for ng-repeat
dalayedChunkHandler.destArray = $scope.yourArrayToDisplay;
dalayedChunkHandler.srcArray = $scope.chatHistoryArray; // the array you got
dalayedChunkHandler.start();