js动态计时器,秒表计时器


先来了解下setInterval() 方法

setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。

setInterval("clock()",1000); //每隔1秒则调用clock()函数一次。

接下来我们看下clock()函数的内容

function clock()
  {
var t;
t=parseInt(document.getElementById("clock").value)+1;
//document.getElementById("clock").value)的意思是获取id为clock元素的value值
//parseInt()函数是将返回的数据转换成int类型
//整句话的意思,就是将id为clock的元素值加1

document.getElementById("clock").value = t;
//改变id为clock的元素值

 t=formatSeconds(t);
//调用formatSeconds()函数,这个函数就是将秒钟换成时分秒的形式

document.getElementById("clocktime").innerHTML=t;
//将id为clocktime的元素的内容赋值为t,即前一秒加1
  } 

整个js特别好理解,就是每个1秒就运行clock()函数一次,将原来的秒数加1.

以下是例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<center>
<input type="hidden" id="clock" value="0">
<span id="clocktime"></span>
<script language=javascript>
function clock()
  {
      var t;
      t=parseInt(document.getElementById("clock").value)+1;
      document.getElementById("clock").value = t;
      t=formatSeconds(t);
      document.getElementById("clocktime").innerHTML=t;
  } 
  
function formatSeconds(value) {
var ctime = Number(value);
        var ctime1 = 0;
        var ctime2 = 0;
        if(ctime > 60) {
            ctime1 = Number(ctime/60);
            ctime = Number(ctime%60);
            if(ctime1 > 60) {
                ctime2 = Number(ctime1/60);
                ctime1 = Number(ctime%60);
            }
        }
        var result = ""+ctime+"秒";
        if(ctime1 > 0) {
            result = ""+parseInt(ctime1)+"分"+result;
        }
        if(ctime2 > 0) {
            result = ""+parseInt(ctime2)+"小时"+result;
        }
        return result;
}
setInterval("clock()",1000);
</script>
</body>
</center>
</html>

PHP Mysql操作增加事务处理


set autocommit=1时。我们提交的SQL是自动提交的。就是跑一句执行保存一句。这样往往会造成第一句对了第二句错的时候执行了一半。来带很多问题。
所以在同时执行2句以上SQL时应该使用事务,需要所有SQL成功后才提交保存。

mysql_query("set autocommit=0"); //设置不自动提交
$res1 = mysql_query($sql1); //sql1
$res2 = mysql_query($sql2); //sql2
if($res1 && $res2){
    mysql_query("COMMIT");  //成功 提交
}else{
    mysql_query("ROLLBACK"); //失败 回滚
}

PHP表单增加token验证,防止站外提交/重复提交/双击提交


原理在于生成一个随机字符串放在session里。提交表单后来验证这个字符串。可以做到防止他人自己写form来欺骗提交,重复提交或者双击提交。






Token.php

<?php

/*
 * Created on 2013-3-25
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
function getToken($len = 32, $md5 = true) {
    # Seed random number generator
    # Only needed for PHP versions prior to 4.2
    mt_srand((double) microtime() * 1000000);
    # Array of characters, adjust as desired
    $chars = array (
        'Q',
        '@',
        '8',
        'y',
        '%',
        '^',
        '5',
        'Z',
        '(',
        'G',
        '_',
        'O',
        '`',
        'S',
        '-',
        'N',
        '<',
        'D',
        '{',
        '}',
        '[',
        ']',
        'h',
        ';',
        'W',
        '.',
        '/',
        '|',
        ':',
        '1',
        'E',
        'L',
        '4',
        '&',
        '6',
        '7',
        '#',
        '9',
        'a',
        'A',
        'b',
        'B',
        '~',
        'C',
        'd',
        '>',
        'e',
        '2',
        'f',
        'P',
        'g',
        ')',
        '?',
        'H',
        'i',
        'X',
        'U',
        'J',
        'k',
        'r',
        'l',
        '3',
        't',
        'M',
        'n',
        '=',
        'o',
        '+',
        'p',
        'F',
        'q',
        '!',
        'K',
        'R',
        's',
        'c',
        'm',
        'T',
        'v',
        'j',
        'u',
        'V',
        'w',
        ',',
        'x',
        'I',
        '$',
        'Y',
        'z',
        '*'
    );
    # Array indice friendly number of chars;
    $numChars = count($chars) - 1;
    $token = '';
    # Create random token at the specified length
    for ($i = 0; $i < $len; $i++)
        $token .= $chars[mt_rand(0, $numChars)];
    # Should token be run through md5?
    if ($md5) {
        # Number of 32 char chunks
        $chunks = ceil(strlen($token) / 32);
        $md5token = '';
        # Run each chunk through md5
        for ($i = 1; $i <= $chunks; $i++)
            $md5token .= md5(substr($token, $i * 32 - 32, 32));
        # Trim the token
        $token = substr($md5token, 0, $len);
    }
    return $token;
}
?>

form.php

<?php
include_once("token.php");
$token = getToken();
session_start();
$_SESSION['token'] = $token;
?>
<form action="action.php" method="post"
<input type="hidden" name="token" value="<?=$token?>" />
<!-- 其他input submit之类的 -->
</form>

action.php

<?php
session_start();
if($_POST['token'] == $_SESSION['token']){
    unset($_SESSION['token']);
    echo "这是一个正常的提交请求";
}else{
    echo "这是一个非法的提交请求";
}
?>

js/jquery验证input里的手机号码


直接上代码
input失去焦点时验证是否为手机号码,如果不是就限制光标在input内

$("#field9").blur(function () {
    phone = this.value;
    RegCellPhone = /^(1)([0-9]{10})?$/;
    falg=phone.search(RegCellPhone);
    if (falg==-1){
    	alert("手机号不合法!");
    	this.focus();
    }
});

上面的例子input的id为field9


Namecheap 最新$0.98域名注册活动 com/net/org/biz


Offer Details
Valid for com/net/org/biz TLDs only
A maximum of one domain per Namecheap account can be registered at this price during the entire promotion period
Coupon usage is limited to 1 per household or business
This is an introductory offer and is valid for new customers only
The price with the coupon code is valid for the first year only, renewal comes at regular price

0.98美元 优惠注册地址
https://www.namecheap.com/promos/0-98-exclusive-domain-promo.aspx
注册域名
com/net/org/biz TLDs only
限新客户 可PP支付