var sec = 1;
function fun() {
    console.log("after", sec++, "seconds");
    if (sec == 6) {
        clearInterval(instance_id);
        //clear interval needs the instance id generated by setinteval function inorder to stop the execution upon a certain condition.
    }
}
var instance_id = setInterval(fun, 1000);//set interval function generates an instance id for a particular instance of a function.

//setTimeout(fun, 3000);
//setInterval(fun, 1000);