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 - How use setTimeout() with Backbone? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How use setTimeout() with Backbone? - Stack Overflow

programmeradmin4浏览0评论

How can I use setTimeout() in a Backbone model? I have the next code:

var ContentModel = Backbone.Model.extend({
URL: "http://localhost/example.php",    
requestType: "POST",
dataType: "json",
data: "", //Set the value outside the model

startSend: function (Data) { 
           //
},
reply: function (Data) { 
    var dataJson = eval(Data);              
    console.log(dataJson);
    setTimeout(this.ajaxRequest(),4000);
},
problems: function (Data) { 
    //
},
ajaxRequest: function () {
    $.ajax({
        async:true,
        type: this.requestType,
        dataType: this.dataType, 
        url: this.URL,
        data: this.data,
        beforeSend:this.startSend,
        success: this.reply,
        timeout:4000,
        error:this.problems 
    });

}       

});

Alternatively I have tried:

setTimeout(function(){
               //ajax code
               },4000);

But the result is the same. setTimeout() don't work. The request only run once.

How can I use setTimeout() in a Backbone model? I have the next code:

var ContentModel = Backbone.Model.extend({
URL: "http://localhost/example.php",    
requestType: "POST",
dataType: "json",
data: "", //Set the value outside the model

startSend: function (Data) { 
           //
},
reply: function (Data) { 
    var dataJson = eval(Data);              
    console.log(dataJson);
    setTimeout(this.ajaxRequest(),4000);
},
problems: function (Data) { 
    //
},
ajaxRequest: function () {
    $.ajax({
        async:true,
        type: this.requestType,
        dataType: this.dataType, 
        url: this.URL,
        data: this.data,
        beforeSend:this.startSend,
        success: this.reply,
        timeout:4000,
        error:this.problems 
    });

}       

});

Alternatively I have tried:

setTimeout(function(){
               //ajax code
               },4000);

But the result is the same. setTimeout() don't work. The request only run once.

Share Improve this question edited May 9, 2013 at 12:49 user2181408 asked Jan 7, 2013 at 23:55 vicenrelevicenrele 9713 gold badges19 silver badges38 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 15

A couple of things are amiss. First off, this line:

setTimeout(this.ajaxRequest(),4000);

Should be:

setTimeout(this.ajaxRequest, 4000);

The first line of code executes the ajaxRequest function and passes the result (which is undefined) to setTimeout. That means the ajaxRequest function will execute once, but too soon. The latter line does what you want, which is to pass the function itself to setTimeout, and ajaxRequest will be called 4 seconds later.

But that's not quite enough. When the ajaxRequest function is executed, the value of this context is incorrect. When you called setTimeout, the context of the callback is set to window. You can verify this by console.log(this) in the callback function.

To fix it, you need to bind the context of the function. Since you're using Backbone, you've also got underscore.js already loaded. Using _.bind oughta do it:

setTimeout(_.bind(this.ajaxRequest, this), 4000);

Edit:

Come to think of it, there might be another problem. When the ajax call succeeds or fails, the reply or problems functions may suffer from the same loss of context as ajaxRequest did. But if there is, it's easily fixed.

Instead of calling _.bind on those too, the simplest way is to call _.bindAll in your Backbone model constructor.

initialize: function() {
  _.bindAll(this, 'ajaxRequest', 'reply', 'problems');
}

When you call _.bindAll, underscore guarantees that every time any of the listed methods of your model is called, the this context variable points to the model itself, unless specifically bound to something else.

You don't need to do anything special to use setTimeout with backbone. Check the scope of this in your reply function. My guess it that this.ajaxRequest() isn't in scope.

You have to use setInverval instead.

setInterval(this.ajaxRequest, 4000);

setTimeout Triggers the function once.

setInterval Triggers each n ms.

var interval = setInterval(this.ajaxRequest, 4000);

clearInterval used to clear setInterval.

clearInterval(interval);

Or pass the context parameter on your ajax:

$.ajax({
    ...
    context: this,
    ...
});
发布评论

评论列表(0)

  1. 暂无评论