www.webdeveloper.com
Results 1 to 7 of 7

Thread: settimeout

Threaded View

  1. #1
    Join Date
    Oct 2010
    Posts
    4

    settimeout

    i've being trying to change the src of a img tag in order to create some kind of a sequence of images.
    Code:
    <script type="text/javascript">
    var i=0;
    function r() {
    	i++;
    	if(i==4){e=1;}
    	var src = "img-" + e + ".jpg"; //images are sorted by number (img-1,img2...)
    	this.src = src;
    	setTimeout(r.call(this),2000);
    	}
    </script>
    ....
    <input type="button"  onclick="r.call(document.getElementById('impar'));">
    <div><img id="impar" src="" ></div>
    the code seems to work, but runs too fast,setTimeout does't look to do anything. if I replace this with this code
    Code:
    <script type="text/javascript">
    var i=0;
    var f=null;
    function r() {
    	i++;
    	if(i==4){e=1;}
    	var src = "img-" + e + ".jpg"; 
    	foo.src = src;
    	setTimeout(r,2000);
    	}
    function init(a){
            f=a;
            r();
            }
    </script>
    ....
    <input type="button"  onclick="init(document.getElementById('impar'));">
    <div><img id="impar" src="" ></div>
    everything works fine.i know call is not the very right thing to been used, but i don't understand why setTimeout dosn't work. some clue?
    Last edited by Kor; 10-26-2010 at 04:56 AM. Reason: wrap the code [code][/code]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles