/    Sign up×
Community /Pin to ProfileBookmark

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>
[/code]

the code seems to work, but runs too fast,setTimeout does’t look to do anything. if I replace [B]this[/B] 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>

[/code]

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?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 26.2010 — Are you sure it works? In your first code: Where have you defined the custom method f.call()?The variable [B] e[/B] is undefined till i==4, so you will encounter an error at the first 3 loops. [B]this[/B] 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 [B]foo[/B]?

What in fact you want to do?
Copy linkTweet thisAlerts:
@utofranzauthorOct 26.2010 — my fault, i made some mistakes copying. the exact code should be:
<i>
</i>&lt;script type="text/javascript"&gt;
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);
}
&lt;/script&gt;
....
&lt;input type="button" onclick="r.call(document.getElementById('impar'));"&gt;
&lt;div&gt;&lt;img id="impar" src="" &gt;&lt;/div&gt;

there is no f.call method.and the second:
<i>
</i>&lt;script type="text/javascript"&gt;
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();
}
&lt;/script&gt;
....
&lt;input type="button" onclick="init(document.getElementById('impar'));"&gt;
&lt;div&gt;&lt;img id="impar" src="" &gt;&lt;/div&gt;

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)
Copy linkTweet thisAlerts:
@KorOct 26.2010 — <i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
function startCarousel(id,i){
var img=document.getElementById(id);
img.src='img-'+i+'.jpg';
i++;
i==4?i=1:null;
setTimeout(function(){startCarousel('impar',i)},2000)
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=""&gt;
&lt;input type="button" value="Start" onclick="startCarousel('impar',1)"&gt;
&lt;div&gt;
&lt;img id="impar" src=""&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@utofranzauthorOct 26.2010 — cute. thanks so much. that's so much easier.
Copy linkTweet thisAlerts:
@utofranzauthorOct 26.2010 — mmm another problem...in your startCarouse, when invoking the settouttime, you refer to a precise element

setTimeout(function(){startCarousel('impar',i)},2000)

what if i want to use the same funtion for a different element, for instance if i have several img tag? i was using [B]this[/B] for this instance.

code like

setTimeout(function(){startCarousel(img,i)},2000) //img is a variable declared before in the function

doesn't work...
Copy linkTweet thisAlerts:
@KorOct 26.2010 — Sorry, my mistake. Should be:
<i>
</i>setTimeout(function(){startCarousel([B][COLOR="Blue"]id[/COLOR][/B],i)},2000)

I really don't understand how you could have used [B]this[/B] 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), [B]this[/B] will refer the Global Window Object.
<i>
</i>&lt;script type="text/javascript"&gt;
function startCarousel(){
alert(this)
}
&lt;/script&gt;
×

Success!

Help @utofranz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...