 
			
				03.06.2019, 10:59
			
			
			
		  
	 | 
 
	
		
		
		
			
			| 
			
				
				
				 Профессор 
				
				
				
				
	
 
 
 
			 | 
			  | 
			
				
				
					Регистрация: 14.03.2010 
					
					
					
						Сообщений: 194
					 
					
					
			
		
 
		 
		
			 | 
		 
		 
		
	 | 
 
	| 
	
	
		
		
			
			 
				Запуск большого количества таймеров.
			 
			
		
		
		
		По очереди через разный промежуток времени срабатывает таймер и стучится в нужные мне функции. 
Я это реализовал так. Всего таких таймеров около 10.
 
setTimeout(function () {
     console.log('test1');
}, 10500);
setTimeout(function () {
     console.log('test3');
}, 14500);
setTimeout(function () {
     console.log('test');
}, 15000);
Подскажите как этот код сделать более красивым.  
		
	
		
		
		
		
		
		
	
		
		
	
	
	 | 
 
 
	 
		 | 
 
 
	
	
	
		
	
		
		
		
			
			 
			
				03.06.2019, 11:49
			
			
			
		  
	 | 
 
	
		
		
		
			  | 
			
			
				
				
				 Профессор 
				
				
				
				
	
 
 
 
			 | 
			  | 
			
				
				
					Регистрация: 27.05.2010 
					
					
					
						Сообщений: 33,150
					 
					
					
			
		
 
		 
		
			 | 
		 
		 
		
	 | 
 
	
	
	
		
		
		
		
		dima85,
  
class Timer {
    constructor(data) {
    this.data = data.map(obj => ({...obj,beginTime : performance.now()}));
    requestAnimationFrame(this.loop.bind(this));
    }
    loop(time){
      this.data = this.data.filter(({beginTime,duration,fn}) => {
         const end = time - beginTime >= duration;
         if(end) {
           fn()
         }
         else return true
      })
      this.data.length && requestAnimationFrame(this.loop.bind(this))
    }
    }
    new Timer([
    {duration : 10500, fn : ()=> console.log('test1')},
    {duration : 14500, fn : ()=> console.log('test3')},
    {duration : 15000, fn : ()=> console.log('test')},
    ])
 
		
	
		
		
		
		
		
		
		
						  
				
				Последний раз редактировалось рони, 05.06.2019 в 12:16.
				
				
			
		
		
	
		
		
	
	
	 | 
 
 
	 
		 | 
 
 
	
	
	
		
	
		
		
		
			
			 
			
				05.06.2019, 10:49
			
			
			
		  
	 | 
 
	
		
		
		
			
			| 
			
				
				
				 Профессор 
				
				
				
				
	
 
 
 
			 | 
			  | 
			
				
				
					Регистрация: 14.03.2010 
					
					
					
						Сообщений: 194
					 
					
					
			
		
 
		 
		
			 | 
		 
		 
		
	 | 
 
	
	
	
		
		
		
		
		Спасибо! 
Подскажите как в new Timer([]); добавить значения с переменной. 
Есть файл test.txt в нем:
 
{duration : 10500, fn : ()=> console.log('test1')},
{duration : 14500, fn : ()=> console.log('test3')},
{duration : 15000, fn : ()=> console.log('test')},
Я его подгружаю на страницу примерно так, в переменную responses у меня попадает все с test.txt  
И далее я пытаюсь это вставить в Timer, но видимо responses надо как-то сказать что в нем массив а не простой текс. Подскажите как это сделать.
 
ajax.get('test.txt',{},function(responses){
     new Timer([responses]);
    },true);
 
		
	
		
		
		
		
		
		
	
		
		
	
	
	 | 
 
 
	 
		 | 
 
 
	
	
	
		
	
		
		
		
			
			 
			
				05.06.2019, 11:17
			
			
			
		  
	 | 
 
	
		
		
		
			  | 
			
			
				
				
				 Профессор 
				
				
				
				
	
 
 
 
			 | 
			  | 
			
				
				
					Регистрация: 27.05.2010 
					
					
					
						Сообщений: 33,150
					 
					
					
			
		
 
		 
		
			 | 
		 
		 
		
	 | 
 
	
	
	
		
		
		
		
		
	
 
	| 
		
			Сообщение от dima85
			
		
	 | 
 
	| 
		test.txt
	 | 
 
	
 
 может сделать json и тогда new Timer(responses);  
		
	
		
		
		
		
		
		
	
		
		
	
	
	 | 
 
 
	 
		 | 
 
 
	
	
	
		
	
		
		
		
			
			 
			
				05.06.2019, 12:16
			
			
			
		  
	 | 
 
	
		
		
		
			
			| 
			
				
				
				 Профессор 
				
				
				
				
	
 
 
 
			 | 
			  | 
			
				
				
					Регистрация: 14.03.2010 
					
					
					
						Сообщений: 194
					 
					
					
			
		
 
		 
		
			 | 
		 
		 
		
	 | 
 
	
	
	
		
		
		
		
		Хорошо, тогда нам надо немного поменять файл, например на такой: 
[{"duration" : 10500, "console":"test1"},
{"duration" : 14500, "console":"test3"},
{"duration" : 15000, "console":"test"}]
Далее:
 
ajax.get('test.txt',{},function(responses){
     new Timer(JSON.parse(responses));
    },true);
А как здесь?:
 
class Timer {
    constructor(data) {
    this.data = data.map(obj => ({...obj,beginTime : performance.now()}));
    requestAnimationFrame(this.loop.bind(this));
    }
    loop(time){
      this.data = this.data.filter({beginTime,duration,fn} => {
         const end = time - beginTime >= duration;
         if(end) {
           fn() //?????
         }
         else return true
      })
      this.data.length && requestAnimationFrame(this.loop.bind(this))
    }
    }
 
		
	
		
		
		
		
		
		
	
		
		
	
	
	 | 
 
 
	 
		 | 
 
 
	
	
	
		
	
		
		
		
			
			 
			
				05.06.2019, 12:22
			
			
			
		  
	 | 
 
	
		
		
		
			  | 
			
			
				
				
				 Профессор 
				
				
				
				
	
 
 
 
			 | 
			  | 
			
				
				
					Регистрация: 27.05.2010 
					
					
					
						Сообщений: 33,150
					 
					
					
			
		
 
		 
		
			 | 
		 
		 
		
	 | 
 
	
	
	
		
		
		
		
		dima85,
  
class Timer {
    constructor(data) {
    this.data = data.map(obj => ({...obj,beginTime : performance.now()}));
    requestAnimationFrame(this.loop.bind(this));
    }
    loop(time){
      this.data = this.data.filter(({beginTime,duration,fn}) => {
         const end = time - beginTime >= duration;
         if(end) {
           fn()
         }
         else return true
      })
      this.data.length && requestAnimationFrame(this.loop.bind(this))
    }
    }
    let responses = `{duration : 10500, fn : ()=> console.log('test1')},
    {duration : 14500, fn : ()=> console.log('test3')},
    {duration : 15000, fn : ()=> console.log('test')}`
    responses = eval(`[${responses}]`)
    new Timer(responses)
 
		
	
		
		
		
		
		
		
	
		
		
	
	
	 | 
 
 
	 
		 | 
 
 
	
	
	
		
	
		
		
		
			
			 
			
				05.06.2019, 12:28
			
			
			
		  
	 | 
 
	
		
		
		
			  | 
			
			
				
				
				 Профессор 
				
				
				
				
	
 
 
 
			 | 
			  | 
			
				
				
					Регистрация: 27.05.2010 
					
					
					
						Сообщений: 33,150
					 
					
					
			
		
 
		 
		
			 | 
		 
		 
		
	 | 
 
	
	
	
		
		
		
		
		dima85,
  
class Timer {
    constructor(data) {
    this.data = data.map(obj => ({...obj,beginTime : performance.now()}));
    requestAnimationFrame(this.loop.bind(this));
    }
    loop(time){
      this.data = this.data.filter(({beginTime,duration,"console" : txt}) => {
         const end = time - beginTime >= duration;
         if(end) {
           console.log(txt)
         }
         else return true
      })
      this.data.length && requestAnimationFrame(this.loop.bind(this))
    }
    }
    let responses =
[{"duration" : 10500, "console":"test1"},
{"duration" : 14500, "console":"test3"},
{"duration" : 15000, "console":"test"}]
    new Timer(responses)
 
		
	
		
		
		
		
		
		
	
		
		
	
	
	 | 
 
 
	 
		 | 
 
 
	
	
	
		
	
		
		
		
			
			 
			
				05.06.2019, 12:33
			
			
			
		  
	 | 
 
	
		
		
		
			  | 
			
			
				
				
				 Профессор 
				
				
				
				
	
 
 
 
			 | 
			  | 
			
				
				
					Регистрация: 27.05.2010 
					
					
					
						Сообщений: 33,150
					 
					
					
			
		
 
		 
		
			 | 
		 
		 
		
	 | 
 
	| 
	
	
		
		
		
		
		 dima85, 
обратите внимание, строка 7 исправлена везде!!! 
		
	
		
		
		
		
		
		
	
		
		
	
	
	 | 
 
 
	 
		 | 
 
 
 
 
 
 
 
	 | 
 
 
 |