javascript - Show Countdown after button click -
i have code html
<div id="container"> <a href="#container">show content</a> <div id="content">i content!</div> </div> how when click show content echo countdown (12s) before show <div id="content">i content!</div>
give me example work. thanks
edit: example updated show elements when countdown starts , hide them when countdown ends. when user clicks on link, counttoshow function invoked. initialy hidden 'ads' element shown command adselement.style.display = '' (which removes display: none style specified inline). when countdown ends (remainingseconds === 0), hide 'ads' element again setting adselement.style.display = 'none'.
here example using setinterval function possibility specify countdown time.
function counttoshow(timeinseconds) { var countdownelement = document.getelementbyid('countdown'); var contentelement = document.getelementbyid('content'); var adselement = document.getelementbyid('ads'); var remainingseconds = timeinseconds; adselement.style.display = ''; countdownelement.innerhtml = remainingseconds; var interval = setinterval(function() { countdownelement.innerhtml = --remainingseconds; if (remainingseconds === 0) { clearinterval(interval); contentelement.style.display = ''; adselement.style.display = 'none'; } }, 1000); } <div id="container"> <a href="#container" onclick="counttoshow(12)">show content</a> <div id="ads" style="display: none"> <div id="countdown"></div> <div>ads</div> </div> <div id="content" style="display: none">i content!</div> </div>
Comments
Post a Comment