html - Show error using javascript - Stack Overflow

Using javascript i want to show error message, and the message will hidedisappear after 2 second . The

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>

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>
Share Improve this question edited Jan 15, 2015 at 14:40 TankorSmash 12.8k6 gold badges70 silver badges108 bronze badges asked Jan 15, 2015 at 14:37 Imranul Hoque LimonImranul Hoque Limon 1563 silver badges11 bronze badges 4
  • 1 What is the event triggering your code (like a button click) or is it loaded with the page itself? – Jaay 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. – Andrew Dunai 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? – Charlie G 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. – Marc B Commented Jan 15, 2015 at 14:43
Add a ment  | 

2 Answers 2

Reset to default 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>

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742351187a4427549.html

相关推荐

  • html - Show error using javascript - Stack Overflow

    Using javascript i want to show error message, and the message will hidedisappear after 2 second . The

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信