这个倒计时,为什么点下去直接变成1了???

laiba_2015 2015-07-24 02:32:22
var countNum = 30;
function sendCount(){
if(countNum > 0){
$("#send").html(countNum);
countNum--;
setTimeout(sendCount(), 1000);
}
}
/**
* 发送短信验证码
*/
$("#send").click(function() {
rewiSendMessage('xgsj','zc');
});

function rewiSendMessage(sendType,channel){
$("#send").addClass('btn-right-gray');
sendCount(30);
}
这个倒计时,为什么点下去直接变成1了??? 没有倒计时效果。
...全文
189 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
starrycheng 2015-08-14
  • 打赏
  • 举报
回复
sendCount()函数声明少写了参数。 不用使用全局变量。
Sasayau 2015-07-27
  • 打赏
  • 举报
回复
引用 8 楼 laiba_2015 的回复:
[quote=引用 1 楼 u012212180 的回复:] setTimeout(sendCount(), 1000); -->setTimeout(sendCount 1000); setTimeout写错了。。具体见: http://stackoverflow.com/questions/31448154/what-is-the-difference-between-settimeoutfn-t-and-settimeoutfn-t
为什么不能加()啊??? 还有不加()的参数函数???[/quote] 。。。给你的链接里说的很清楚啊
laiba_2015 2015-07-24
  • 打赏
  • 举报
回复
引用 1 楼 u012212180 的回复:
setTimeout(sendCount(), 1000); -->setTimeout(sendCount 1000); setTimeout写错了。。具体见: http://stackoverflow.com/questions/31448154/what-is-the-difference-between-settimeoutfn-t-and-settimeoutfn-t
为什么不能加()啊??? 还有不加()的参数函数???
wangji8171 2015-07-24
  • 打赏
  • 举报
回复
function sendCount(countNum){ if(countNum > 0){ $("#send").html(countNum); countNum--; setTimeout(sendCount(), 1000); } } /** * 发送短信验证码 */ $("#send").click(function() { rewiSendMessage('xgsj','zc'); }); function rewiSendMessage(sendType,channel){ $("#send").addClass('btn-right-gray'); sendCount(30); } 我给你方法加了一个变量取消了全局变量 LZ试试
sydhr_1994 2015-07-24
  • 打赏
  • 举报
回复
setTimeout(sendCount, 1000); 这样写就好了
  • 打赏
  • 举报
回复
好多地方有问题 你的click方法里面是sendCount(30),但实际你方法根本没参数,用的又是全局的参数,可以将你的方法改下
function sendCount(nowNum){       
        if(nowNum> 0){
            nowNum--;
            $("#send").html(nowNum);
            setTimeout(function(){sendCount(nowNum)}, 1000);     
        }else{
             alert('倒计时结束了');
       }
    }
Sasayau 2015-07-24
  • 打赏
  • 举报
回复
另外可以看下这篇帖子:http://www.codesec.net/view/167547.html
Braska 2015-07-24
  • 打赏
  • 举报
回复
setTimeout('sendCount()', 1000); 加引号 function rewiSendMessage(sendType,channel){ $("#send").addClass('btn-right-gray'); sendCount(); //参数去掉 }
Sasayau 2015-07-24
  • 打赏
  • 举报
回复
而且。。。sendCount(30); 参数传不进去啊
Sasayau 2015-07-24
  • 打赏
  • 举报
回复
setTimeout(sendCount(), 1000); -->setTimeout(sendCount 1000); setTimeout写错了。。具体见: http://stackoverflow.com/questions/31448154/what-is-the-difference-between-settimeoutfn-t-and-settimeoutfn-t

87,922

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧