'use strict';

var syncTime = require('../frameloop/sync-time.js');
var frame = require('../frameloop/frame.js');

/**
 * Timeout defined in ms
 */
function delay(callback, timeout) {
    const start = syncTime.time.now();
    const checkElapsed = ({ timestamp }) => {
        const elapsed = timestamp - start;
        if (elapsed >= timeout) {
            frame.cancelFrame(checkElapsed);
            callback(elapsed - timeout);
        }
    };
    frame.frame.setup(checkElapsed, true);
    return () => frame.cancelFrame(checkElapsed);
}

exports.delay = delay;
