Jump to content

Want to pause a section of my code... How?


StefanRSA

Recommended Posts

Hi, I am working on an image gallery that display thumbnails and display bigger image onclick. I now have an animated gif that display until the image is loaded.

 

I want the animated give to display for about 1.5sec before the bigger image display. What should I do to my code to pause or have a delay before the big image are loaded?

 

My Code in head is as follow:

<script type="text/javascript"> 
// Preload the loading image 
var loadingImg = new Image(); 
loadingImg.src = 'images/wait.gif'; 
function changeImage(filename) 
{ 
// Show the loading image 
document.mainimage.src = loadingImg.src; 
var bigImg = new Image(); 
bigImg.onload = function() { 
// When the big image finishes loading, swap the main img with it 
document.mainimage.src = bigImg.src; 
} 
bigImg.src = filename; 
} 
</script>

And in body:

<a href="javascript:changeImage('img/main1.jpg')">
<img src="img/thumb1.jpg">
</a><a href="javascript:changeImage('img/main2.jpg')">
<img src="img/thumb2.jpg">
</a><a href="javascript:changeImage('img/main3.jpg')">
<img src="img/thumb3.jpg">
</a><br>
<img src="" name="mainimage" alt="" />

Link to comment
Share on other sites

Thanks, I am new to JS and not sure where or how to put it.

Tried this but now the big image is not loaded after the wait.gif is activated? The poison killed my code  :P

 

<script type="text/javascript"> 
// Preload the loading image 
var loadingImg = new Image(); 
loadingImg.src = 'images/wait.gif'; 
function changeImage(filename) 
{ 
// Show the loading image 
document.mainimage.src = loadingImg.src; 

var bigImg = setTimeout(new Image(),3); // TESTING THE DELAY ----- NOT WORKING
bigImg.onload = function() { 
// When the big image finishes loading, swap the main img with it 

document.mainimage.src = bigImg.src; 
} 
bigImg.src = filename; 
} 
</script> 

Link to comment
Share on other sites

And if I do this, the main image display but no delay....?

 

<script type="text/javascript"> 
// Preload the loading image 
var loadingImg = new Image(); 
loadingImg.src = 'images/wait.gif'; 
function changeImage(filename) 
{ 
// Show the loading image 
document.mainimage.src = loadingImg.src; 
var bigImg = new Image(); 
bigImg.onload = function() { 
// When the big image finishes loading, swap the main img with it 
setTimeout(document.mainimage.src = bigImg.src,3000);
} 
bigImg.src = filename; 
} 
</script> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.