Home Reference Source

src/utils/TimerUtils.js

import {useEffect} from 'react';

/**
 * Receives parts of assessment, and returns timer delay values
 * based on true or false
 * @param {boolean} grading - True if the timer delay is for grading
 * @param {boolean} comment - True if the timer delay is for free text
 * @param {boolean} drawing - True if the timer delay is for drawing
 * @returns {number[]} Array of timer delay values as ints [gradingDelay, commentDelay, drawingDelay, complimentDelay]
 */
function smileyometerAnimationDelay(grading, comment, drawing) {
    //Initial states
    let gradingDelay = 0;
    let commentDelay = 0;
    let drawingDelay = 0;
    let complimentDelay = 1000;

    // If there is a grading, set delay to 1000
    if (grading) gradingDelay = 1000;

    // If there is a comment, set delay to gradingDelay + 1000
    if (comment) commentDelay = gradingDelay + 1000;
    else commentDelay = gradingDelay;

    // If there is a drawing, set delay to commentDelay + 1000
    if (drawing) drawingDelay = commentDelay + 1000;
    else drawingDelay = commentDelay;

    // Add the total delay to complimentDelay
    complimentDelay += drawingDelay;

    return [gradingDelay, commentDelay, drawingDelay, complimentDelay];
}

/**
 * A timed event. Runs function after time
 * @param {function} func - Function to be executed after time
 * @param {number} time - Time before function should execute
 * @param {number} id - Id of event
 */
function timedEvent(func, time, id) {
    useEffect(() => {
        let timer = setTimeout(() => func, time);
        return () => clearTimeout(timer);
    }, [id]);
}

export {smileyometerAnimationDelay, timedEvent};