admin管理员组文章数量:1336631
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
本文标签: htmlShow error using javascriptStack Overflow
版权声明:本文标题:html - Show error using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742351186a2458525.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论