简体   繁体   中英

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>

Use setTimeout function to solve it:

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

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; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM