开发者

How to Hide an Image, if not clicked, in N seconds?

开发者 https://www.devze.com 2023-04-09 23:20 出处:网络
I have a button that when is clicked, an imageis created using javascript and get prepended to a div (adds it inside a div).

I have a button that when is clicked, an image is created using javascript and get prepended to a div (adds it inside a div).

var image = new Image();
var imageHtml = image.toHtml();
$('div.board').prepend(imageHtml);

function Image()
{

this.toHtml = function ()
{
return '<开发者_Go百科img src=\"myImage.png\" width=\"40px\" height=\"40px\" />';
}
}

This image can be clicked in 2 seconds then user will have 1 more score and if not clicked in that time, then the image should disappear.

How to do that in javascript?

Thanks,


function start_game(image){
    var timeout = null;
    image.onclick = function(){
        clearTimeout(timeout);
        //addScore();
    };
    timeout = setTimeout(function(){  
        image.onclick = null;
        image.style.display = "none";
        // remove the image from dom if needed;
    }, 2000);
}  

Demo: http://jsfiddle.net/UpNCb/


see this, just difference is you going to hide instead of link display

or give id 'img_id' to your image

function hideimage() {
  document.getElementById('img_id').style.display = 'none';
}

setTimeout(hideimage, 10000);


Use the function setTimeout() and the CSS property display or visibility.

0

精彩评论

暂无评论...
验证码 换一张
取 消