Good afternoon, you need to have a script that opens a hidden div by clicking on the link. Need to make a discovery with a delay of 3 seconds, how to do it? settimeout before the function does not work.

<!DOCTYPE html> <meta charset="utf-8"> <head> <title>Задание 2</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <a href="#" onclick="show('div')">ссылка</a> <div id="div"> Произвольный текст. </div> <script src="main2.js"></script> </body> 

Js

 function show(id) { document.getElementById(id).style.display = 'block'; } 
  • setTimeout (show, 3000), works. how did you do? - ddeadlink
  • Yes, he did as indicated in the answers, tried several times, apparently somewhere was wrong. Thanks for answers. - Stepan

3 answers 3

 function show(id) { setTimeout(function(){ document.getElementById(id).style.display = 'block'; },3000) } 

     function show(id) { setTimeout(function() { document.getElementById(id).style.display = 'block'; }, 3000); } 
     <a href="#" onclick="show('div')">ссылка</a> <div id="div" style="display:none"> Произвольный текст. </div> 

      More as an option

       var link = document.querySelector('.link'), alert = document.querySelector('.alert'); link.onclick = function(e) { e.preventDefault(); alert.classList.add('alert-show'); } 
       .alert { opacity: 0; visibility: hidden; padding: 8px 15px; background: tomato; color: #fff; display: inline-block; border-radius: 3px; transition: 0s; } .alert-show { opacity: 1; visibility: visible; transition-delay: 3s; } 
       <a href="#" class="link">ссылка</a> <div class="alert"> Произвольный текст. </div> 

      • ++ preventDefault () - NeedHate