找回密码
 立即注册
搜索
查看: 1734|回复: 0

[基础语法] JS点击元素执行倒计时

[复制链接]

581

主题

110

回帖

4066

积分

管理员

积分
4066

众神之神

发表于 2016-3-15 20:48:19 | 显示全部楼层 |阅读模式
源代码
  1. $(function() {
  2.     'use strict';
  3.     $(document).on('pageInit', ".superpage_shop", function(e, id, page) {
  4.         //点击获取验证码
  5.         $('.btn_verify', page).click(function() {
  6.             var t = this;
  7.             var timer, seconds = 60;
  8.             if ($(t).hasClass('button-default')) {
  9.                 $(t).removeClass('button-default').addClass('button-light disabled').html('获取验证码('+seconds+')');
  10.                 timer = setInterval(function(){ //每隔多少之间执行函数内的东西
  11.                     seconds -= 1;
  12.                     if (seconds < 0) {
  13.                         $(t).removeClass('button-light disabled').addClass('button-default').html('获取验证码');
  14.                         clearInterval(timer);
  15.                         return;
  16.                     }
  17.                     $(t).html('获取验证码('+seconds+')');
  18.                 }, 1000);
  19.             }
  20.         });
  21.     });
  22. });
复制代码
  1. $(document).on('pageInit', ".superpage_shop", function(e, id, page) {   //在当前页面下执行+
复制代码
  1. $('.btn_verify', page).click(function() {      //点击clsss=btn_verify的时候执行点击事件
复制代码
  1. var timer, seconds = 60;    //timer:定时器功能,econds = 60:要定时的时间长度;
复制代码
  1. timer = setInterval(function(){ //setInterval每隔多少时间(1000毫秒)执行函数内的东西
复制代码
  1. seconds -= 1;   //要定时的总时间自减1秒;
复制代码
  1. if (seconds < 0) {  //当所剩时间小于0的时候
复制代码
  1. clearInterval(timer);   //清除当前执行的时间
复制代码
  1. $(t).html('获取验证码('+seconds+')');    //将html展示出来
复制代码
  1. }, 1000);   //setInterval每隔1000毫秒执行函数内的东西
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|十三博客 ( 鲁ICP备2023000528号 )

GMT+8, 2026-6-1 17:28 , Processed in 0.050789 second(s), 23 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表