Are you sure it works? In your first code: Where have you defined the custom method f.call()?The variable e is undefined till i==4, so you will encounter an error at the first 3 loops. this will not refer the image object but either the Global Window Object, or the input element.
In your second version: Where have you defined the variable foo?
my fault, i made some mistakes copying. the exact code should be:
Code:
<script type="text/javascript">
var i=0;
function r() {
i++;
if(i==4){i=1;}
var src = "img-" + i + ".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>
there is no f.call method.and the second:
Code:
<script type="text/javascript">
var i=0;
var f=null;
function r() {
i++;
if(i==4){i=1;}
var src = "img-" + i + ".jpg";
f.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>
i need to change the source of an img tag, just to let have a kind of presentation of the images. the code do works, indeed if i add an alert() to show the this.src, the source do change, a sort of loop is set up, and the images shown do change, but this happens just to fast, so that is possible to see the changing only with the alert message. the strange speed of the loop is visible also if i combine this loop with some other tween, lasting 2sec too (settimeout is set with 2000msec):with the alert() on, the tween doesn't even starts; alert off and tween goes on, but the img tag goes stuck to a certain image (and the fact that in different browser(firefox,safari,chrome) the image is not the same makes me think the loop is on, but runs too quick)
Last edited by Kor; 10-26-2010 at 04:57 AM.
Reason: wrap the code [code][/code]
I really don't understand how you could have used this the way you said. If: a function is not a constructor or a function was not fired dynamically via an new created event or a function is not an anonymous inner function (none of them was in your case), this will refer the Global Window Object.
Code:
<script type="text/javascript">
function startCarousel(){
alert(this)
}
</script>
Bookmarks