Edit in JSFiddle

$(document).ready(function () {
    function assert(value, desc) {
        var res = $("#results");
        var li = document.createElement("li");
        li.className = value ? "pass" : "fail";
        li.appendChild(document.createTextNode(desc));
        res.append(li);
    }

    // There are four methods for timers in javascript
    // setTimeout & clearTimeout - called as id = setTimeout(fn, delay) - calls fn after the delay(ms) only once. The id it returns can be used to clear the timeout as in clearTimeout(id)
    // setInterval & clearInterval - called as id = setInterval(fn, delay) - keeps calling fn after delay until clearInterval is called as in clearInterval(id)
    // since only a single thread is doing the execution for javascript, the time after which the timer will get called is not fixed. If some other code is running in javascript and the timer duration has elapsed, the time will wait for the other code to finish executing and then will execute its handler.

    // Difference between timeout and intervals
    // the difference is that intervals might get executed back to back if they get backed up enough however setTimeout in the below example will always execute after the stipulated delay
    // also multiple instances of same interval handler will never be queued up

    // setTimeout sample
    var timeOutBox = $("#timeOutBox"),
        x = 0,
        y = 0;

    function timeoutFn() {
        console.log("timeoutFn called");
        x = x + 100;
        y = y + 100;
        timeOutBox.css("height", x + "px");
        timeOutBox.css("width", y + "px");
    }

    timeOutBox.css("height", "10px");
    timeOutBox.css("width", "10px");
    var timeoutId = setTimeout(timeoutFn, 3000);
    console.log("timeout id : " + timeoutId);

    // setInterval sample
    var intervalBox = $("#intervalBox"),
        increment = 10;

    function intervalFn() {
        console.log("intervalFn called");
        var ht = parseInt(intervalBox.css("height"), 10);
        ht = ht + increment;
        var wt = parseInt(intervalBox.css("width"), 10);
        wt = wt + increment;
        intervalBox.css("height", ht + "px");
        intervalBox.css("width", wt + "px");

        if (ht > 100) clearInterval(intervalId);
    }

    intervalBox.css("height", "10px");
    intervalBox.css("width", "10px");
    var intervalId = setInterval(intervalFn, 1000);
    console.log("interval id : " + intervalId);
});
<span>setTimeout sample - Fires once after 3 seconds</span>

<div id="timeOutBox" class="square"></div>
<br/>
<br/> <span>setInterval sample - Fires multiple times after 1 second</span>

<div id="intervalBox" class="square"></div>
<br/>
<div id="results" />
body {
    margin:10px;
}
.pass {
    color:green;
}
.fail {
    color:red;
    text-decoration:line-through;
}
.square {
    border:red solid 2px;
}