Madhan Kumar
Madhan Kumar

Reputation: 23

javascript tags for link on image with timeout 3 secs

I am trying to put a link on the image using JavaScript tags with a timeout function of 3 secs,

The js code

var img = new Image();
img.src = 'http://test.com/cdb/smail_images/TVSmilesLogo.jpg';
img.onclick = function() {
    window.location.href = 'http://test.com/';
};
document.body.appendChild(img);

and html file code is

<script src="t.js" type="text/javascript"></script>

Upvotes: 0

Views: 1343

Answers (2)

godblessstrawberry
godblessstrawberry

Reputation: 5058

all you need is wrap your redirect into setTimeout() function. also I suggest to change mouse cursor via css, so user will know the image is clickable

var img = new Image();
img.src = 'http://lorempixel.com/350/350/';
document.body.appendChild(img);
img.onclick = function() {
  setTimeout(function() {
    window.location.href = 'http://test.com/';
  }, 3000);
};
body{
  text-align: center;
  }

img {
  cursor: pointer;
  opacity: 0.8;
  transition: opacity 0.4s ease;
}

img:hover{
opacity: 1;
}

Upvotes: 0

Cai Yongji
Cai Yongji

Reputation: 348

Use setTimeout function to solve it:

setTimeout(function() {
   alert("do something");
},3000);//3000 means msec

Upvotes: 2

Related Questions