Показать сообщение отдельно
  #8 (permalink)  
Старый 26.02.2014, 08:59
Профессор
Отправить личное сообщение для kostyanet Посмотреть профиль Найти все сообщения от kostyanet
 
Регистрация: 23.10.2010
Сообщений: 2,718

Корявенько, но принип должен быть понятен:

// friends_data came from somewhere
var count_friends=50;
var interval=334;
var th=null;
var ids=[]
var city_count=[];

function getMutual() {

	if(!friends_data.length) { // end of, but unsaved data will be lost
		if(th)
			clearTimeout(th);
		return false; 
	}
	
	friend=friends_data.shift(); // get first elem and reduce array
	friendid=friend.uid;
	var ts = new Date().getTime(); // time start

	VK.api("friends.get",{user_id:friendid,fields:"city"},function(data) {

		if (data.response) {

			var sum = 0;
			for (var i=0;i<data.response.length;i++) 
				if(data.response[i].city == 2) 
					sum++;

			ids.push(friendid);
			city_count.push(sum);

			if (ids.length >= count_friends && city_count.length >= count_friends){
				if(th)
					clearTimeout(th);
				$.post(vbazy, { res:ids, res2:city_count}, function(data) {
					ids=[];
					city_count=[];
					th = setTimeout(getMutual, interval);
					console.log("Занесли");
				});
			}
			else {
				var tn = new Date().getTime();
				var td = interval+ts-tn;
				if(td<0)
					td=1;
				th = setTimeout(getMutual, td); // set remaining time to next call
			}

		}
	});

}

getMutual();

Последний раз редактировалось kostyanet, 26.02.2014 в 09:02.
Ответить с цитированием