0

I'm writing this jquery code :

$('form').after('<p id="suc"></p>');
$('#suc').html('success !');
$('#suc').show(700);
setTimeout(function(){$('#suc').hide('slow')},2500);
$('#suc').remove();

When i remove $('#suc').remove(); like this :

$('form').after('<p id="suc"></p>');
$('#suc').html('success !');
$('#suc').show(700);
setTimeout(function(){$('#suc').hide('slow')},2500);  

The code run succefuly, but when i put it, it dosen't run !!

What the problem with that ? it's illegal to but $('#suc').remove(); here ?

4 Answers 4

1

The setTimeout call doesn't wait for the callback to run before the code continues, so you will be removing the element immediately. When the code in the callback tries to hide the element, it's not there.

Remove the element in the complete callback of the hide method:

setTimeout(function(){
  $('#suc').hide('slow', function(){
    $('#suc').remove();
  });
},2500);
Sign up to request clarification or add additional context in comments.

4 Comments

thank you in advance,it's now running..but i need more explaination : what happend is that the whole code not running not only that last tow sentences ! so what the reason of that ?
@walid: The element will be removed even before the show animation runs.
but why !!? is it running asynchronously ?
@walid: Yes, all animations are asynchronous. There are no updates in the browser while your function is running, so animations have to be asynchronous.
1

As you're using hide you're also safe to use delay, so:

$('#suc').show(700).delay(2500).hide('slow', function () {
  $(this).remove();
});

will suffice.

demo: http://jsbin.com/isediz/2/


Also, as a bit of clarification, regarding:

The code run succefuly, but when i put it, it dosen't run !!

Actually the code runs (in a sense), the problem is that your remove will not wait for the two asynchrones events (setTimeout and .hide('slow')). So it will get executed way before those two are done doing what they should do.

Comments

0

You need to put the remove() inside of the setTimeout and in the callback for the hide() function:

$('form').after('<p id="suc"></p>');
$('#suc').html('success !');
$('#suc').show(700);

setTimeout(function() { 
    $('#suc').hide('slow', function() { $(this).remove(); })
}, 2500);

Comments

0

You are using the element setTimout callback which you have already removed with the statement just after setTimout. The call back of setTimeout will execute after the statement removing element with id #suc by the next statement of setTimeout. Remove #suc in hide callback so that it is not accessed by script after removal.

$('form').after('<p id="suc"></p>');
$('#suc').html('success !');
$('#suc').show(700);
setTimeout(function(){
      $('#suc').hide('slow', 
           function(){$(this).remove();
      }); 
},2500);

2 Comments

I dont think so, setInterval is for 25 seconds here.
It's setTimeout, not setInterval.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.