te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - setInterval in jQuery happens too fast - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setInterval in jQuery happens too fast - Stack Overflow

programmeradmin3浏览0评论

I use setInterval and sometimes it happens "too fast". Here how it looks:

setInterval(function() {
    //here es ajax functions and so on. 
}, 1000);   

Sometimes setInterval happens faster than all those ajax functions and it gives me two messages instead of one. What a solution to this?

I use setInterval and sometimes it happens "too fast". Here how it looks:

setInterval(function() {
    //here es ajax functions and so on. 
}, 1000);   

Sometimes setInterval happens faster than all those ajax functions and it gives me two messages instead of one. What a solution to this?

Share Improve this question asked Apr 15, 2011 at 21:02 good_eveninggood_evening 21.7k69 gold badges198 silver badges306 bronze badges 1
  • 1 Maybe you shouldn't be using setInterval at all. You might want to fire off those calls, then use setTimeout to schedule the calls again once they have e back. – mahemoff Commented Apr 15, 2011 at 21:09
Add a ment  | 

6 Answers 6

Reset to default 10

It's hard to tell what you're running into, the question is a bit unclear.

setInterval is great for some things, but not for anything where you're going to be mixing other asynchronous stuff in with it. Instead, use the "rescheduling setTimeout" idiom:

setTimeout(doSomething, 1000);
function doSomething() {
    $.ajax("your_url", {
        success: function() {
            // Do something here

            // Do something else here
        },
        plete: function() {
            // Previous run plete, schedule the next run
            setTimeout(doSomething, 1000);
        }
    });
}

...because, after all, your ajax call may take more than a second to plete.

If that's not the problem you're having, my guess is your code looks something like this:

setInterval(function() {
    $.ajax("your_url", {
        success: function() {
            // Do something here
        }
    });

    // Do something else here
}, 1000);

...and you're wondering why the "Do something else here" code is running before the "Do something here" code. If so, the reason is that by default, ajax calls are asynchronous. Your call to $.ajax starts the call, but that's all; then all your other code runs before the success (or error) callbacks occur.

The fix, of course, is to not do anything else at the top level that relies on the success callback:

setInterval(function() {
    $.ajax("your_url", {
        success: function() {
            // Do something here

            // Do something else here
        }
    });

}, 1000);

With jQuery 1.5.x you can use the Then() for deferred object. This is a nice way to say once you are done then() do this. You can also use the When() option to have it wait for more than one ajax request to plete.

These two things are very cool and powerful.

http://api.jquery./deferred.then/

http://api.jquery./jQuery.when/

Set a flag that indicates that the ajax fetches are in process. When all of the ajax fetches plete, clear the flag. At the top of your setInterval function, return immediately if the flag is set.

It's better not to use setInterval, but to set a fresh setTimeout each time. For example:

setTimeout(function ajaxStuff() {
    // here es ajax functions and so on.

    setTimeout(ajaxStuff, 1000);
}, 1000);

Of course, if the functions within are asynchronous, as AJAX requests normally are, the setTimeout call will still e too soon. You'll need to write some code that calls setTimeout when the requests are plete. $.when helps you with this, since $.ajax and other jQuery AJAX methods implement $.Deferred:

setTimeout(function ajaxStuff() {
    $.when(
        $.ajax({
            url: 'ajax1.htm'
        }),
        $.ajax({
            url: 'ajax2.htm'
        }),
        $.ajax({
            url: 'ajax3.htm'
        })
    ).done(function() {
        setTimeout(ajaxStuff, 1000);
    });
}, 1000);

I think the problem here is due to scope. Eventhough the method is triggered successfully.

With similar problem I have able to use this to fix:

setTimeout(function(){
    load1();
}, 5000); 

function load1 () {
    console.log('loaddd1..');
    setTimeout(load2(), 4000); 
}

function load2 () {
    setTimeout(function(){
    console.log('end load2');
}, 4000); 

had this issue and clearInterval wasn't working.

make sure setInterval is only called once by wrapping it in an if statement:

var interval;

if (typeof(interval) === 'undefined') {
  interval = setInterval(actFakeData,3000);
}

also helpful for me was assigning setInterval to a variable and console.log it so you can see the value throughout your code. for me when it was speeding up it was increasing in numeric value instead of resetting until I wrapped it in this.

发布评论

评论列表(0)

  1. 暂无评论