3

I want to "show" the hidden div after 2-3 sec delay. Is it possible in the same javascript? Help me please.......

I've used the following code:

function ShowSecond() {
    var div2 = document.getElementById("div2");
    div2.className = "show";
    setTimeout(function () {
        div2.className = 'hide';
    }, 2000);
}
.show {
      -o-transition: opacity 3s;
      -moz-transition: opacity 3s;
      -webkit-transition: opacity 3s;
      transition: opacity 3s;
      opacity:1;
  }
  .hide {
      opacity:0;
  }
<!DOCTYPE html>
<html>
    
    <head>
        <style>
</style>
        <script>
            
        </script>
    </head>
    
    <body>
        <div id="div1" class="show">First Div
            <button onclick="ShowSecond()">clickMe</button>
        </div>
        <div id="div2" class="hide">Hidden Div</div>
    </body>

</html>

I want to "show" the hidden div after 2-3 sec delay

vishnu
  • 2,441
  • 2
  • 21
  • 44

8 Answers8

1

/* 1. First you have to implementation on document ready function

  1. Then delay function execution by (xxxx) ms

    $(document).ready(function() { setTimeout(function() { //what ever you want yo delay }, xxxx); }); */

OR You will find the Answer here

Community
  • 1
  • 1
developerbh
  • 180
  • 9
  • Just updated the answer .. someone asked that before – developerbh Aug 03 '16 at 05:39
  • Is it the right method?------ function ShowSecond() { var div2 = document.getElementById("div2"); div2.className = "show"; setTimeout(function () { div2.className = 'hide'; }, 2000); //added the following function ShowSecond() { $(document).ready(function() { setTimeout(function() { 2 }, ShowSecond); }); } – vishnu Aug 03 '16 at 05:42
1

use css property transition-delay: 3s; in show class

nktkarnany
  • 31
  • 1
  • 11
1

If you want transition you will have to use some kind of css transition(or alot of javascript calls, if your browser doesnt support transitions).

Here a mini example, how to perform the transition with only javascript
Speed and duration for this typ of transition has to be done the the interval(here 250 ms) and the opacity step-size (here 0.1)
(With this settings the duration for showing the div is about 2.5 seconds, since 250ms * 10 = 2.5s

function startShowing(){
  var elementToHide = document.getElementById("show");
  elementToHide.style.opacity = 0;
  var intervalId = setInterval(function(){
    if(elementToHide.style.opacity >= 1)
    {
      clearInterval(intervalId);
    }else{
      elementToHide.style.opacity = parseFloat(elementToHide.style.opacity) + 0.1;
    }
  },250);
}

//startShowing();
.hide{
opacity:0;
}
<div class="hide" id="show"> show me </div>
<button onclick="startShowing()"> SHOW </button>

If you mean something else please specify your problem in more detail.

OLD Answer, probably not the intended solution

Just add a plus-sign and a space when setting the hide css class to the div

With other words change this div2.className = 'hide'; to this div2.className += ' hide'; . Like this both classes are set on the div but the opacity value is overriden by the last added class

function ShowSecond() {
    var div2 = document.getElementById("div2");
    div2.className = "show";
    setTimeout(function () {
        div2.className += ' hide';
    }, 2000);
    
}
.show {
      -o-transition: opacity 3s;
      -moz-transition: opacity 3s;
      -webkit-transition: opacity 3s;
      transition: opacity 3s;
      opacity:1;
  }
  .hide {
      opacity:0;
  }
<!DOCTYPE html>
<html>
    
    <head>
        <style>
</style>
        <script>
            
        </script>
    </head>
    
    <body>
        <div id="div1" class="show">First Div
            <button onclick="ShowSecond()">clickMe</button>
        </div>
        <div id="div2" class="hide">Hidden Div</div>
    </body>

</html>

Like this the transition of the show is still valid and only the opacity is overriden.

winner_joiner
  • 3,643
  • 4
  • 32
  • 48
1

Try out this:

function ShowSecond() {

    setTimeout(function () {
        document.getElementById("div2").style.visibility = "hidden";
    }, 2000);
}
Meena
  • 99
  • 2
1

Have you tried to create another function to call your function with your desired delay?

function ClickShowSecond(){
    setTimeout(ShowSecond, 2000);
}
0

Just add one more setTimeout and add delay accordingly.

function ShowSecond() {
    var div2 = document.getElementById("div2");
    div2.className = "show";
    setTimeout(function () {
        div2.className = 'hide';
    }, 2000);
    setTimeout(function () {
        div2.className = 'show';
    }, 5000);
}
.show {
      -o-transition: opacity 3s;
      -moz-transition: opacity 3s;
      -webkit-transition: opacity 3s;
      transition: opacity 3s;
      opacity:1;
  }
  .hide {
      opacity:0;
  }
<!DOCTYPE html>
<html>
    
    <head>
        <style>
</style>
        <script>
            
        </script>
    </head>
    
    <body>
        <div id="div1" class="show">First Div
            <button onclick="ShowSecond()">clickMe</button>
        </div>
        <div id="div2" class="hide">Hidden Div</div>
    </body>

</html>
Gaurav Aggarwal
  • 8,921
  • 5
  • 27
  • 65
0
setTimeout(function () {
        div2.className = 'hide';
        setTimeout(showDiv,3000);
    }, 2000);

function showDiv(){
    var div2 = document.getElementById("div2");
    div2.className = 'show';
}
Rohan Veer
  • 670
  • 6
  • 16
0

this works on me, you can put the function in button trigger or when the page is load

// set this SetTimeout to your event trigger or in document ready 

setTimeout(showsomething, 3000);

function showsomething() {
//make your div or element being show here
}
Evinn
  • 155
  • 11