我想倒计时然后验证码

时间:2014-06-10 10:27:02

标签: javascript captcha countdowntimer

您好 我得到了倒计时代码

 <script type="text/javascript">

window.onload = function() {
    countDown('my_div1', '<form>1+1=<input name="d" type="text" /></form>', 10);
}
function countDown(elID, output, seconds) {
    var elem = document.getElementById(elID),
        start = new Date().getTime(), end = start+seconds*1000,
        timer = setInterval(function() {
            var now = new Date().getTime(), timeleft = end-now, timeparts;
            if( timeleft < 0) {
                elem.innerHTML = output;
                clearInterval(timer);
            }
            else {
                timeparts = [Math.floor(timeleft/60000),Math.floor(timeleft/1000)%60];
                if( timeparts[1] < 10) timeparts[1] = "0"+timeparts[1];
                elem.innerHTML = "Time left: "+timeparts[1];
            }
        },250); 
}
</script>

我想在倒计时结束时出现CAPTCHA或者问题是否正确,然后继续链接

2 个答案:

答案 0 :(得分:0)

尝试在此处设置倒计时功能

window.onload = function() {
countDown('my_div1', //here// , 10);

}

答案 1 :(得分:-2)

假设您的倒计时编码工作正常并且您有一个存储验证码的div说div id = "captchadiv"

 <script type="text/javascript">

window.onload = function() {
//////////////////////////////////////////////////////
set the visibility of the captcha hidden here.
//////////////////////////////////////////////////////

    countDown('my_div1', '<form>1+1=<input name="d" type="text" /></form>', 10);
}
function countDown(elID, output, seconds) {
    var elem = document.getElementById(elID),
        start = new Date().getTime(), end = start+seconds*1000,
        timer = setInterval(function() {
            var now = new Date().getTime(), timeleft = end-now, timeparts;
            if( timeleft < 0) {
                elem.innerHTML = output;
                clearInterval(timer);
//////////////////////////////////////////////////////
              set visibility of captchadiv to visibile.
//////////////////////////////////////////////////////

            }
            else {
                timeparts = [Math.floor(timeleft/60000),Math.floor(timeleft/1000)%60];
                if( timeparts[1] < 10) timeparts[1] = "0"+timeparts[1];
                elem.innerHTML = "Time left: "+timeparts[1];
            }
        },250); 
}

//////////////////////////////////////////////////////
add a function here to validate the captcha. 
If validation succeeds, do success action.
If validation fails, set captcha visibility to hidden and again call the counttDown function.
//////////////////////////////////////////////////////


</script>

修改 看一下这个。 (未经测试的版本)

 <script type="text/javascript">
var mathenticate;
window.onload = function() {
    countDown('my_div1', '<form>1+1=<input name="d" type="text" /></form>', 10);
}
function countDown(elID, output, seconds) {
    var elem = document.getElementById(elID),
        start = new Date().getTime(), end = start+seconds*1000,
        timer = setInterval(function() {
            var now = new Date().getTime(), timeleft = end-now, timeparts;
            if( timeleft < 0) {
                elem.innerHTML = output;
                clearInterval(timer);
             mathenticate = {
            bounds: {
                lower: 5,
                upper: 50
            },
            first: 0,
            second: 0,
            generate: function()
            {
                this.first = Math.floor(Math.random() * this.bounds.lower) + 1;
                this.second = Math.floor(Math.random() * this.bounds.upper) + 1;
            },
            show: function()
            {
                return this.first + ' + ' + this.second;
            },
            solve: function()
            {
                return this.first + this.second;
            }
            };


            mathenticate.generate();

            var $auth = $('<input type="text" name="auth" />');
            $auth
            .attr('placeholder', mathenticate.show())
            .insertAfter('input[name="name"]');

            }
            else {
                timeparts = [Math.floor(timeleft/60000),Math.floor(timeleft/1000)%60];
                if( timeparts[1] < 10) timeparts[1] = "0"+timeparts[1];
                elem.innerHTML = "Time left: "+timeparts[1];
            }
        },250); 
}


    $('#form').on('submit', function(e){
        e.preventDefault();
        if( $auth.val() != mathenticate.solve() )
        {
            alert('wrong answer!');
        // If you want to generate a new captcha, then
         mathenticate.generate();
        }else {
            document.location.href = 'http://www.overdir.com';

    }
    });


</script>