admin管理员组文章数量:1312722
i want to disable button on click for few seconds, and show an image during this time, then hide the image and show the button again.
HTML:
<input type="submit" value="Submit" onclick="validate();" name="myButton" id="myButton">
<img style="display: none;" src="/images/loading.gif" id="myImage">
JavaScript:
function validate(){
var myButton= document.getElementById('myButton');
var myImage= document.getElementById('myImage');
setTimeout (function(){
myButton.style.display ='none';
},5000);
setTimeout (function(){
myImage.style.display ='inline';
},5000);
}
ISSUE:
This js code is not executed onclick, but after the action of this button is invoked (using JSF 2)
When this code is invoked, the button gets hidden and the image appears, but never get's hidden again, and the button is not getting displayed again.
please advise how to fix that.
i want to disable button on click for few seconds, and show an image during this time, then hide the image and show the button again.
HTML:
<input type="submit" value="Submit" onclick="validate();" name="myButton" id="myButton">
<img style="display: none;" src="/images/loading.gif" id="myImage">
JavaScript:
function validate(){
var myButton= document.getElementById('myButton');
var myImage= document.getElementById('myImage');
setTimeout (function(){
myButton.style.display ='none';
},5000);
setTimeout (function(){
myImage.style.display ='inline';
},5000);
}
ISSUE:
This js code is not executed onclick, but after the action of this button is invoked (using JSF 2)
When this code is invoked, the button gets hidden and the image appears, but never get's hidden again, and the button is not getting displayed again.
please advise how to fix that.
Share Improve this question edited Jan 11, 2012 at 13:35 BalusC 1.1m376 gold badges3.6k silver badges3.6k bronze badges asked Jan 11, 2012 at 9:50 Mahmoud SalehMahmoud Saleh 33.6k123 gold badges350 silver badges512 bronze badges 1- Submit will reload the page or submit the form - you need to change it to a button or target the form to another frame/iframe/window or use ajax – mplungjan Commented Jan 11, 2012 at 10:01
4 Answers
Reset to default 6You're actually hiding the button and showing the image after 5 seconds, then not changing it back. Try this:
function validate(){
var myButton= document.getElementById('myButton');
var myImage= document.getElementById('myImage');
myButton.style.display ='none';
myImage.style.display ='inline';
setTimeout (function(){
myButton.style.display ='inline';
myImage.style.display ='none';
},5000);
}
Perhaps you mean
setTimeout (function(){
myButton.style.display ='none';
setTimeout (function(){
setTimeout (function(){
myButton.style.display ='none';
},5000);
myImage.style.display ='inline';
},5000);
},5000);
You added code only for hiding the button and displaying the image. The function should look like this:
function validate(){
var myButton= document.getElementById('myButton');
var myImage= document.getElementById('myImage');
var hide_timeout = 5000; // delay 5 sec before hide button;
var show_timeout = 10000; // delay 10 sec before show button;
setTimeout (function(){
myButton.style.display ='none';
myImage.style.display ='inline';
},hide_timeout);
setTimeout (function(){
myButton.style.display ='inline';
myImage.style.display ='none';
},show_timeout);
}
Minor tweaks:
function validate() {
var myButton= document.getElementById('myButton');
var myImage= document.getElementById('myImage');
myButton.style.visibility='hidden';
myImage.style.display ='inline';
setTimeout (function(){
myImage.style.display ='none';
myButton.style.visibility ='visible';
},5000);
return false;
}
本文标签: javascriptHide button for few seconds onclickStack Overflow
版权声明:本文标题:javascript - Hide button for few seconds onclick - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741907723a2404248.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论