jQuery ajax 和 setTimeout clearTimeout同时使用的方法

 <ul class="ncc-payment-list">
       <li payment_code="alipay">
	   支付宝
        </li>
       <li payment_code="wxpay">
	   微信
       </li>
 </ul>

<script type="text/javascript">
//http://phpff.com
$(function(){
    $('.ncc-payment-list > li').on('click',function(){
		if($(this).attr('payment_code')=='wxpay'){
			//开始监听
			Wechatpay();
			times=0;
		}else{
			//停止监听
			clearTimeout(WechatpayId);
		}
    });

var WechatpayId=null;
var times=1;
	function Wechatpay(){
		//alert(times);
		WechatpayId=setTimeout(function () {
			$.ajax({
				url: "wechatpay.php",
				type: "POST",
				data: {pay_sn:'310502302562152075'},
				dataType: 'json',  
				success: function (result) {
					if(result==1){
						times=0;
						alert('ok');
					}else{
						times=1;
					}
					//console.log(result);
				},
				complete: Wechatpay
			});
		}, 2101);

		//停止请求
		if(times==0){
			clearTimeout(WechatpayId);
		}
	}

});
  </script>

jquery 使用 settimeout 每两秒触发一次 ajax请求

function load() {
    setTimeout(function () {
        $.ajax({
            url: "includes/handlechat.php",
            type: "GET",
            data: data,
            dataType: 'json',  
            success: function (result) {
                $("#clog").empty();
                $.each(result, function (rowKey, row) {
                    $("#clog").append('<p ><h4>' + row.username + ':</h4>' + row.message_content + '</p>');
                });
            },
            complete: load
        });
    }, 1101);
}
load();

也可以使用IIFE的方式:

(function load() {
   // setTimeout here
}());

php使用setTimeout和window.location实现url自动跳转

php在采集或者生成静态页时经常要用到这种方法,使用setTimeout和window.location就可以轻松实现url自动跳转。
<?php
//http://phpff.com/makeHtml.php?i=1
define('ROOT',dirname(__FILE__));
include( ROOT.'/global.php' );
$job=new act();
$i=isset($_GET['i'])?intval($_GET['i']):0;
$r=$job->makeHtml($i,10);
if($r){
    $i+=10;
    echo '<script type="text/javascript">
            setTimeout(\'window.location.href="./makeHtml.php?i='.$i.'";\',2000);
         </script>';
}
?>