weixin_33701251 2011-03-04 16:32 采纳率: 0%
浏览 89

AjAX调用和setInterval

I'm trying to add pagination. I use

    $(document).ready(function() {
var pageNum = 1;
$(".paginationing").click(function(){
pageNum = $(this).attr("id");
});


    setInterval("ajaxd("+pageNum+")",5000);

});

function ajaxd(pageNum) { 


    var thisuser = $("#thisusern").text();
  $.ajax({
   type: "GET",
   url: "newstitle.php",
   data: "user="+thisuser+"&page="+pageNum,
   success: function(msg){
     $("#edix").html(msg);
   }
 });
} 

To send the page number.But it sends only page number 1 ,even I click on the page number. Where's the bug?How can I implement AJAX pagination with setInterval to call the page every 5 seconds?

  • 写回答

1条回答 默认 最新

  • weixin_33709219 2011-03-04 16:38
    关注

    Am not sure why are you using setInterval to paginate instead of calling the ajaxd function in the click event handler. I would do it this way:

    $(document).ready(function() {
        var pageNum = 1;
        $(".paginationing").click(function() {
                    pageNum = $(this).attr("id");
                    ajaxd(pageNum);
        });
    });
    
    function ajaxd(pgNo) {
            var thisuser = $("#thisusern").text();
        $.ajax({
            type: "GET",
            url: "newstitle.php",
            data: "user=" + thisuser + "&page=" + pgNo,
            success: function(msg) {
                $("#edix").html(msg);
            }
        });
    }
    

    If you still want to use the setInterval try this:

       var pageNum = 1;
    
        $(document).ready(function() {
    
            $(".paginationing").click(function() {
                pageNum = $(this).attr("id");
            });
            setInterval(ajaxd, 5000);
        });
    
        function ajaxd() {
                var pgNo = pageNum;
            var thisuser = $("#thisusern").text();
            $.ajax({
                type: "GET",
                url: "newstitle.php",
                data: "user=" + thisuser + "&page=" + pgNo,
                success: function(msg) {
                    $("#edix").html(msg);
                }
            });
        }
    

    P.S: Also note that a valid id for a HTML element should not start with a number but I think you are relying on that. I would rather advice you to generate the id something like "pg-n" where n is the number and in the click event you can get the page number using

    pageNum = $(this).attr("id").replace(/[^0-9]/g, "");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名