replace setInterval with setTimeout

thanks in advance for your help! I am working with (and super new to) JavaScript, node.js with express, and sqlite3. I am trying to make an AJAX request to get a list of all the messages that have been posted to the chatroom page:

var meta = document.querySelector('meta[name=roomName]');
var roomName = meta.content;

window.addEventListener('load', function(){
     var intervalID = setInterval(updateMessages, 4000);
}, false);

function updateMessages() {
    var req = new XMLHttpRequest();
    req.open('GET', '/' + roomName + '/messages.json', true);
    req.send();
    document.getElementById('messages').innerHTML = req.responseText;
}

Two questions: 1. I think I should be using setTimeout instead of setInterval. How would I go about switching to using this method? 2. Is the server-side code below that corresponds to the code above correct? How do I get access to the data that comes back after this request?

app.get('/:roomName/messages.json', function(request, response){
    var roomName = request.params.roomName;
    var sql = "SELECT ALL body FROM messages where room="+roomName+";";
    conn.query(sql, function(error, result) { 
      if(error) {
        console.log("There was an error.");
      }
      response.send(result);
    });
});

setInterval is the appropriate thing to use here.

However, keep in mind that you will never see any messages because AJAX is asynchronous, so req.responseText won't have anything. You should use a readystatechange event:

req.open(......);
req.onreadystatechange = function() {
    if( this.readyState == 4) {
        document.getElementById('messages').innerHTML = this.responseText;
    }
};
req.send();