Interval Timer Example

by DaveAlger

HTML

<body>
    <p id="status">waiting <span id="t"></span> seconds for the page to load...</p>
    <iframe id="myframe" src="http://bing.com"></iframe>
</body>

CSS

body{ background: #eee; }
p{font-family: sans-serif; font-size:24px; color: crimson;}
iframe{ width: 90%; height: 300px; border: 3px dashed crimson; background: #fff;}

JavaScript

var timer;
var waitDelay = 5;

$(document).ready( function() {
    $('#t').text( waitDelay );
    timer = setInterval(updateLabel,1000);
});

function updateLabel() {
    if ( waitDelay <= 0 ) {
        $('#status').text( 'assume the page finished loading and try to authenticate' );
        clearInterval(timer);
        tryToLogin();
    } else {
        $('#t').text( --waitDelay );
    }
}

function tryToLogin() {
    $('#myframe').attr('src','http://bing.com?q=hello');
}