3

Using javascript i want to show error message, and the message will hide/disappear after 2 second . The error show perfectly and hide after 2 second but it does not work for the second time. if i reload my page it work perfectly again and so on.

JavaScript

if(task_hour == "hour" || task_minute == "minute"){

        document.getElementById("error").innerHTML = "Add Time for the Task";
        setTimeout(function(){ document.getElementById("error").style.display="none"; }, 2000);

        return false;
    }

HTML

<div id="errordiv" align="center" style="margin-left: auto; margin-right: auto;"> 
   <span id="error" style="color: red"> </span> 
</div>
4
  • 1
    What is the event triggering your code (like a button click) or is it loaded with the page itself? Commented Jan 15, 2015 at 14:39
  • How exactly do you show it? You should set display: <smth> to show it and display: none to hide. Commented Jan 15, 2015 at 14:40
  • How is your javascript being called...? is it in a function? or is this just running on page load? Commented Jan 15, 2015 at 14:40
  • 1
    Of course it won't work. After the first error, the element's style is none, and you never reset it to block to make it visible again. Commented Jan 15, 2015 at 14:43

2 Answers 2

5

You should set the div to be initially hidden (display: none) and use display: block to show it:

JS:

var timer = null;

function showError(message) {
    if (timer !== null) {
        // Clear previous timeout:
        clearTimeout(timer);
        timer = null;
    }
    var errorElement = document.getElementById("error");
    errorElement.innerHTML = message;
    errorElement.style.display = 'block';
    timer = setTimeout(function(){ errorElement.style.display = 'none'; }, 2000);
}

showError('Error!');

HTML:

<div id="errordiv" align="center" style="margin-left: auto; margin-right: auto;"> 
    <span id="error" style="color: red; display: none"></span> 
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

what will happen if i add 1 error and 1 more after 1 second...?
Fair point. You could grab the handle for the existing setTimeout and cancel it if a new one is created. I think you'd probably use a global variable to hold it though, without any further context.
@KirillPisarev I've edited my answer to include this situation.
@AndrewDunai first it creates global variable that is bad practise, second, you may show only 1 message... see
0

The second error appears in hiden div

you have to create a span, append it to wrapper with error text and after 2 seconds destroy it:

function showError(message){
    var span = document.createElement('span');
    var errorWrap = document.getElementById("error");

    span.appendChild(document.createTextNode(message));
    errorWrap.appendChild(span);

    setTimeout(function(){ span.parentNode.removeChild(span); }, 2000);

    return false;
}

if(task_hour == "hour" || task_minute == "minute"){
    showError('Add Time for the Task');
}

see

1 Comment

would you mind to provide a bit more informative answer?

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.