我正在尝试使用带有3秒超时功能的JavaScript代码在图像上添加链接
js代码
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);
和html文件代码是
<script src="t.js" type="text/javascript"></script>
答案 0 :(得分:2)
使用setTimeout
功能解决问题:
setTimeout(function() {
alert("do something");
},3000);//3000 means msec
答案 1 :(得分:0)
您只需将重定向包装到setTimeout()
函数中即可。我还建议通过css更改鼠标光标,这样用户就会知道图像是可点击的
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);
};
&#13;
body{
text-align: center;
}
img {
cursor: pointer;
opacity: 0.8;
transition: opacity 0.4s ease;
}
img:hover{
opacity: 1;
}
&#13;