Javascript: running setInterval only once

Possible duplicate:
setInterval - How to run only once?

I would like to run the following code only once, so after 2 seconds it will change the iframe src, but it won't try to do it over and over again.

<script type="text/javascript">
    setInterval(function () {document.getElementById('iframe').src = "http://www.y.com";}, 2000);
    </script>

      

+3


source to share


3 answers


You are looking for setTimeout()

one that does exactly that.



+12


source


Yes...

window.setTimeout(function(){
        // code to run after 5 seconds...
}, 5000);

      

or by moving your method to an external context



function myMethod(){
    // code to run after 5 seconds...
};

window.setTimeout(myMethod, 5000);

      

The latter is useful if you have a method that you do not plan to execute ONLY with this timeout.

+5


source


Use setTimeout , you can see more details on the Mozilla website.

+1


source







All Articles