The example in this article describes the countdown button implemented in js. Share it with everyone for your reference. The specific analysis is as follows:
The rendering is as follows:
The specific code is as follows:
<span style="font-size: 14px;"> <input type="button" value="确定"id="rulesubmit" /> <scripttypescripttype="text/javascript"> var secs = 3; var wait = secs * 1000; var update = function(num,value){ if (num == (wait/1000)){ $("#rulesubmit").val("免费获取"); } else{ printnr = (wait/1000) - num; $("#rulesubmit").val("免费获取(" + printnr +")"); } }; var timer = function(){ $("#rulesubmit").attr("disabled",false); $("#rulesubmit").val("免费获取"); }; $(function(){ (function(){ function getValidateCode(){ $("#rulesubmit").val("免费获取(" + secs +")"); $("#rulesubmit").attr("disabled",true); for (i = 1; i <= secs;i++){ window.setTimeout("update(" + i + ")",i*1000); } window.setTimeout("timer()",wait); } $("#rulesubmit").click(function(){ getValidateCode(); }); })(); //注意,我这里在测试的时候改成里匿名函数,其实不必这样做也可以实现 //getValidateCode()当作一个单独的函数,在$(function(){//点击按钮执行函数,即上面蓝色部分代码;}); }); </script></span>
This is just a small example. In actual application, various times may need to be recorded. Taking into account changes in page refresh, cookies need to be used for data storage, etc.!
I hope this article will be helpful to everyone’s JavaScript programming design.