JQUERY刷新div代码需要超时

I have this code to refresh a certain div on my site for the chat;

var refreshId = setInterval(function() {
    $('#chat_grab').load('chat_grab.php?randval=' + Math.random());
}, 5000);

However if someone leaves a page open, this will loop forever. Is there any way to change it so it times out if the base page hasnt been refreshed in say 10 minutes?

var timer = new Date();
var refreshId = setInterval(function() {
    $('#chat_grab').load('chat_grab.php?randval=' + Math.random());
    if((new Date() - timer) > (10 * 60 * 1000)) {
        clearInterval(refreshId);
    }
}, 5000)