How do you auto logout user after session cookie expires while taking a test?

I'm building an MVC ExpressJS Test app, and am trying to auto logout user after they stop answering test questions. I keep getting the "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client". I have a logout button function that works within the app, but automating it seems to not work.

This is the chunk code that's not working within the Question Controller:

  setInterval(function() {
    if (req.session.cookie.maxAge <= 0) {
      clearInterval(this);
      req.session.destroy(err => {
        console.log("Time's up, logging out user!");
        if (err) throw err;

        res.clearCookie(config.sessionData.SESS_NAME);
        console.log("redirecting..");
        return res.render('login_user');
      })  
    } else {
      console.log("Time left: ", req.session.cookie.maxAge / 1000, "seconds left");
    }
  }, 10000);

This is the code of the whole controller:

const pool = require('../models/db');
const config = require('../config');

module.exports = (req, res) => {
  let time_start;
  let qTotal;
  let mcLimit;
  let question_id = req.session.question_id;
  let quiz_id = req.session.quiz_id;

  setInterval(function() {
    if (req.session.cookie.maxAge <= 0) {
      clearInterval(this);
      req.session.destroy(err => {
        console.log("Time's up, logging out user!");
        if (err) throw err;

        //res.clearCookie(config.sessionData.SESS_NAME);
        console.log("redirecting..");
        return res.render('login_user');
      })  
    } else {
      console.log("Time left: ", req.session.cookie.maxAge / 1000, "seconds left");
    }
  }, 10000);
  
  console.log("Question_id: " + question_id);
  console.log("Quiz_id: " + quiz_id);
  pool.query(
      `SELECT Quiz_id, Question_id, Prompt From Govt_questions WHERE Question_id = ?;
      SELECT Answer_prompt From Govt_answers Where Question_id = ?;
      SELECT * FROM Govt_answers WHERE Question_id = ?;
      SELECT Answer_id FROM Govt_answers WHERE Question_id = ?;
      SELECT Question_id From Govt_questions`
      ,[question_id, question_id, question_id, question_id]
      )
    .then(rows => {
      if (question_id > qTotal) {
        res.redirect('progress-report');
      } else {
        //temp is correct answers
        var temp = [];
        var count = rows[1].length;
        var govtQuestions = rows[0][0];
        var govtAnswersId = rows[3];
        var govtAnswers = rows[2];
        quiz_id = govtQuestions['Quiz_id'];
        qTotal = rows[4].length;
        req.session.time_start = new Date().toISOString().slice(0, 19).replace('T', ' ');
        console.log("JSON?: " + JSON.stringify(govtAnswers));

        for (var i = 0; i < count; i++) {
          temp.push(rows[2][i].Correct.toJSON().data[0]);//this loops through all answers and pushes binary data as true and false answers
        }
        mcLimit = temp.reduce((total, val) => {
          return total + val;
        });

        return res.render('show_questions', {
          quizId: govtQuestions['Quiz_id'],
          questionNum: govtQuestions['Question_id'],
          question: govtQuestions['Prompt'],
          answerId: govtAnswersId,
          answers: govtAnswers,
          mcLimit: mcLimit,
          questionTotal: qTotal,
          userName: req.session.user_name
        });
      }
    })
    .catch(err => {
      console.log(err);
    })
};
How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum