beforeLoad 事件超时

发布于 2024-12-05 02:01:02 字数 1102 浏览 0 评论 0原文

我正在尝试编写一些 ExtJS 4 脚本。我有以下代码:

var companyStoreModel = Ext.create('Ext.data.Store', {
        model: 'CompanyDataModel',
        proxy: {
            type: 'ajax',
            url: _company_load_url,
            reader: 'json',
        },
        listeners: {
            beforeload: function(store, options) {
                            loadFunction(0);
                        },
            load: function(store, records, options) {
                      $('section#test').html('test: data loaded');
                  },
        },
        autoLoad: true,
    });

在脚本的开头我有:

var _timeout;
var loadFunction = function(no)
{
    $('section#test').html('test: is loading');
    for (var i = 0; i<no; i++)
    {
        $('section#test').append('.');
    }
    _timeout = setTimeout(loadFunction(no++), 100);
}

所以我想向用户显示数据正在加载。数据加载良好,一切正常,无需 beforeload 事件。这段代码在

_timeout = setTimeout(loadFunction(no++), 100);

调用时给我 jQuery 错误。错误是:超出最大调用堆栈大小。有人可以给我提示如何做这样的事情或者我做错了什么吗?

I'm trying to write some ExtJS 4 script. I have the following code:

var companyStoreModel = Ext.create('Ext.data.Store', {
        model: 'CompanyDataModel',
        proxy: {
            type: 'ajax',
            url: _company_load_url,
            reader: 'json',
        },
        listeners: {
            beforeload: function(store, options) {
                            loadFunction(0);
                        },
            load: function(store, records, options) {
                      $('section#test').html('test: data loaded');
                  },
        },
        autoLoad: true,
    });

and at the begining of the script I have:

var _timeout;
var loadFunction = function(no)
{
    $('section#test').html('test: is loading');
    for (var i = 0; i<no; i++)
    {
        $('section#test').append('.');
    }
    _timeout = setTimeout(loadFunction(no++), 100);
}

So I want to show to the user that data is being loaded. Data is loaded well and everything works without beforeload event. This code gives me jQuery error when

_timeout = setTimeout(loadFunction(no++), 100);

is called. The error is: Maximum call stack size exceeded. Can someone give me a hint how to do such thing or what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2024-12-12 02:01:02

现在,您的函数正在立即执行(导致巨大的执行调用堆栈)。您不能将其放入 setTimeout 函数中。您可以仅传递函数名称(可以评估的代码),也可以使用匿名函数:

名称:

// You will need to put no in the global scope for this.
_timeout = setTimeout(loadFunction, 100); 

Eval:

_timeout = setTimeout("loadFunction("+no+")", 100); 

Anon。功能。

_timeout = setTimeout(function() {
    loadFunction(no++);
}, 100);

Right now, your function is being executed straight away (causing a huge call stack of executions). You cannot put it in the setTimeout function like that. You either pass ONLY the function name, a code that can be eval'd or you use an anonymous function:

Name:

// You will need to put no in the global scope for this.
_timeout = setTimeout(loadFunction, 100); 

Eval:

_timeout = setTimeout("loadFunction("+no+")", 100); 

Anon. func.

_timeout = setTimeout(function() {
    loadFunction(no++);
}, 100);
宫墨修音 2024-12-12 02:01:02

加载数据时,您必须clearTimeout

        load: function(store, records, options) {
                  $('section#test').html('test: data loaded');
                  clearTimeout(_timeout);
              },

You have to clearTimeout when data is loaded:

        load: function(store, records, options) {
                  $('section#test').html('test: data loaded');
                  clearTimeout(_timeout);
              },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文