Показать сообщение отдельно
  #6 (permalink)  
Старый 05.06.2019, 12:22
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,068

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)
Ответить с цитированием