-1

Im trying to make a script in which an image appears for the user after 10 minutes. Think of it as a timer. And once the timer reaches 0, an image appears. Im pretty new to javascript, so I really have no idea where to start. Does anyone know of a script which will do the job, or maybe point me in the right direction? Thanks.

user796855
  • 9
  • 1
  • 1
  • 3

7 Answers7

4

HTML:

<img src="yourimage.jpg" id="yourImageId" style="display: none" />

JS:

setTimeout(function() { document.getElementById('yourImageId').display('block'); }, 10 * 60 * 1000);

with setTimeout you set the timer to trigger after 10 * 60 * 1000 ms ( = 10 minutes); It triggers the function and display's the hidden image. Fore tidyness put your css for hiding the image in a proper css file.

fijter
  • 17,607
  • 2
  • 25
  • 28
  • thanks, but what do you mean by imageID? And how can i go about making the image "clickable"? Like how can i link it to another page? – user796855 Jun 18 '11 at 17:32
  • yourImageId is the id attribute of your HTML element. So you put a around the image and change yourImageId by newlink in the javascript to show the linked image after 10 minutes. – fijter Jun 18 '11 at 17:34
4

This is how you should add your image to your HTML

<img id="myimage" src="" style="display:none" />

Here is your JavaScript:

<script type="text/javascript">
setTimeout(function(){
    document.getElementById('myimage').style.display = 'block';
},600000);
</script>

You can test it here

Shef
  • 44,808
  • 15
  • 79
  • 90
1

Can you repeat this for every image in a slideshow? I have tried your code to blackout for, say 500 ms, the transition between previous image and the next image to prevent their concatenation. Since the (random) images have a very different aspect ratio, I do not use (cross)fading here. Now, your code only works once, right at the start of the show, and then the images are concatenated again. My piece of code

#blackit { display: none; }  
<body>

<script>

document.write('<img id="blackit" onload="resizeImage()" margin="0" border="0" alt="" src="" '+rimages[Math.floor(Math.random()*(rimages.length))]+'">')


var timeoutID = setTimeout(function() { document.getElementById('blackit').style.display        ='block'; }, 500); 

var paused = false;

document.onkeydown = function(evt) {
    evt = evt || window.event;
    if (evt.keyCode == 32) {
        paused = !paused;
    }
};


(function loop() { 
var  rantime = Math.round(Math.random() * 7000) + 1000;
timeoutID; 
setTimeout(function() { 
   if (!paused) { 
       rotateImage(); 
       }
    loop();
   }, rantime);
})();
user2317959
  • 71
  • 1
  • 1
  • 6
0

Have a look at setTimeout. You can hide the image initially (with CSS display: none;) and after the timeout finished, show the image.

Example:

var timeout = setTimeout(function() {
    document.getElementById('imageID').style.display = 'block';
}, 10000); // of course you have to set the right time

As you want to detect user inactivity, also have a look at Detecting idle time in JavaScript elegantly and others.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

Look for setTimeOut method in javascript if you can use jQuery 1.4+ it has jQuery delay function.

sushil bharwani
  • 29,685
  • 30
  • 94
  • 128
0

You could use the setTimeout() function to achieve this. The function takes two parameters, a function to run and the time to delay. 10 minutes = 600 seconds which works out as 600,000 milliseconds, which is the timescale JS uses. So you could do something like this, if you were using jQuery of course:

setTimeout(function() {
    $("#my-img").show();
},600000);

Or if not do it with regular JS using:

setTimeout(function() {
    document.getElementById("my-img").style.display = "block";
}, 600000);
Jack Franklin
  • 3,765
  • 6
  • 26
  • 34
0
setTimeout(function(){
    // do some stuff here, like add an image for example...
}, (60 * 10 * 1000))
// seconds * minutes * 1000
Billy Moon
  • 57,113
  • 24
  • 136
  • 237