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 anddisplay: 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 toblock
to make it visible again. – Marc B Commented Jan 15, 2015 at 14:43
2 Answers
Reset to default 5You 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
评论列表(0条)