倒计时获取验证码、事件代码

-(void)sendSMS{
    
    if(isPhone == YES){
          isPhone = NO;
        __block int timeout=60; //倒计时时间
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
        dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
        dispatch_source_set_event_handler(_timer, ^{
            if(timeout<=0){ //倒计时结束,关闭
                dispatch_source_cancel(_timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    //设置界面的按钮显示
                    [smsBtn setTitle:@"获取" forState:UIControlStateNormal];
                    smsBtn.userInteractionEnabled = YES;
                });
            }else{
                int seconds = timeout % 120;
                NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                dispatch_async(dispatch_get_main_queue(), ^{
                    //设置界面的按钮显示
                    //NSLog(@"____%@",strTime);
                    [UIView beginAnimations:nil context:nil];
                    [UIView setAnimationDuration:1];
                    [smsBtn setTitle:[NSString stringWithFormat:@"%@",strTime] forState:UIControlStateNormal];
                    [UIView commitAnimations];
                    smsBtn.userInteractionEnabled = NO;
                });
                timeout--;
            }
        });
        dispatch_resume(_timer);
    }else{
        hud = [[MBProgressHUD alloc] initWithView:registerView];
        [registerView addSubview:hud];
        hud.mode = MBProgressHUDModeText;
        [hud setOffset:CGPointMake(0, -200.0f)];
        hud.label.text = @"手机号有误";
        [hud showAnimated:YES];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            sleep(1);
            dispatch_async(dispatch_get_main_queue(), ^{ //回到主线程
                [hud removeFromSuperview];
            });
        });
        
    }
}

时间: 2024-10-09 20:59:36

倒计时获取验证码、事件代码的相关文章

javascript 倒计时获取验证码

var wait=60;function reSendCode(id) { var obj = $("#"+id); if (wait == 0) { obj.attr("disabled",false); $('#getcode').removeClass('no-disable').addClass("get-code").attr('disabled',false).val("免费获取验证码"); wait = 60;

倒计时获取验证码

演示: 代码: RegisterActivity.java import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.Button; import com.jialianjia.bzw.BaseActivity; import com.jialianjia.bzw.R; import com.jialianjia.bzw.u

clips 前端 js 倒计时 获取验证码的按钮

1 <a href="javascript:void(0);" onclick="get_captcha()" class="btn btn-default btn6" id="for_captcha" >获取验证码</a> 2 3 <script type="text/javascript"> 4 5 var url="/get-verify-code?pho

JS监听checkbox的选择获取取消事件代码案列

function OncheckBox(index){ if ($(index).attr("checked") == "checked") { alert($(index).val()); alert("选中"); } //取消 else { alert("取消"); } } <input type="checkbox" id="payTypes1" name="pay

vue获取验证码倒计时,自定义组件点击事件不生效的问题

说明:部分组件使用的是element-ui 子组件 <template> <div class="count-down"> <el-button type="primary" size="small" style="width:80px;" :disabled="disabled || time > 0"> {{ text }} </el-button>

iOS项目中获取验证码倒计时及闪烁问题解决方案

大家在做验证码的时候一般都会用到倒计时,基本上大家实现的方式都差不多,先贴出一些代码来.. -(void)startTime{ __block int timeout= 59; //倒计时时间 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE

获取验证码效果和后台代码

客户端js+html代码 <script type="text/javascript"> var tcode = 0;//定时器返回代码 //获得验证码 function GetVerifyCodeAction() { var email = $("#email").val(); if (!checkEmail(email)) { $("#area_error").addClass("log-tips").show

Android实战简易教程-第四十一枪(显示倒计时的Button-适用于获取验证码)

近期在做获取验证码的功能.考虑到优良的用户体验,决定制作一个拥有倒计时提示的Button按钮,在网上查了一些资料,非常是简单的就能实现.我写了一个小Demo,大家能够应用到自己的项目中. 一.代码 1.activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/to

Angular.js 使用获取验证码按钮实现-倒计时

获取验证码界面效果如图: 需要实现以下逻辑 按钮不可选 --输入电话号码,按钮可选 --点击获取,进入倒计时,按钮不可选 --倒计时结束,回到初识状态 核心代码: var cd = 60; var toDo = function() { cd--; $scope.countDown = "重新获取 " + cd; }; $interval(toDo, 1000, 60); 完整代码: html: <form name="form" class="fo