Chymmi
Chymmi

Reputation: 151

Remove Div On Click With Timeout

I have this javascript on click to remove div on click, but it doesnt work at all :(

Can you please help me ? I would be so happy (I already tried to search over the other questions)

There is JS

onclick="setTimeout('$('#wait').remove()', 11000);"

Upvotes: 1

Views: 417

Answers (4)

Jamiec
Jamiec

Reputation: 136239

Rather than have some javascript inline in an onclick you should use .delay and .queue from jQuery.

$('#clickme').on('click', function(){
  $('#wait').delay(11000).queue(function(){
    $(this).remove().dequeue()
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wait">Gone in 11 Seconds</div>
<button id="clickme">Click me to start the countdown</button>

Upvotes: 0

Ross Brasseaux
Ross Brasseaux

Reputation: 4151

I think pretty much all of these solutions would work. Here is another perhaps more elegant version of what you are doing up there, Chymmi.

JsFiddle Demo: https://jsfiddle.net/kvvbbz6e/4/


Javascript

$(document).ready(function(){
    //--------------------------------------------------------------
    // this is what you would need
    var waitButton = $('#wait'),
        waitButtonTimer;

    waitButton.on('click',function(){ // clicking this a second time will reset the timer.
        clearInterval(waitButtonTimer);

        waitButtonTimer = setTimeout(function(){
            waitButton.off('click');
            $('.infolabel').text('click event unbound');
        }, 4000);

    });
    //--------------------------------------------------------------
});

HTML

<div id="wait" class="button">Wait Button</div>
<span class="infolabel">Click event bound</span>

CSS

.button {
    display: inline-block;
    color: #666;
    height: 24px;
    font-size: 9.5pt;
    font-weight: bold;
    line-height: 22px;
    border: 0;
    padding: 0 5px;
    margin: 0;
    border: 1px solid rgba(0, 0, 0, 0.2);
    border-radius: 4px;
    background: rgb(255,255,255); /* Old browsers */
    background: -moz-linear-gradient(top, rgba(255,255,255,1) 60%, rgba(245,245,245,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(60%,rgba(255,255,255,1)), color-stop(100%,rgba(245,245,245,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, rgba(255,255,255,1) 60%,rgba(245,245,245,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, rgba(255,255,255,1) 60%,rgba(245,245,245,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, rgba(255,255,255,1) 60%,rgba(245,245,245,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom, rgba(255,255,255,1) 60%,rgba(245,245,245,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#f5f5f5',GradientType=0 ); /* IE6-9 */
    box-shadow: 0px 1px 1px #fff;
    cursor: pointer;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
}

Upvotes: 0

moonknight
moonknight

Reputation: 334

Wrong syntax and quotes usage. This:

onclick = "setTimeout(function() { $('#wait').remove() }, 11000);";

would be correct.

Upvotes: 1

Paul Roub
Paul Roub

Reputation: 36458

Note the nested single quotes in your handler. That won't end well. What's the browser supposed to do with this?

'$('#wait').remove()'

You really want to define a function and use that instead. You avoid all of the pitfalls of passing a string to setTimeout(), multi-level quoting, etc.

function hideit() {
  $('#wait').remove();
}

// ...

<button onclick="setTimeout(hideit, 11000);">click me</button>

Upvotes: 0

Related Questions