简体   繁体   English

访问socket.io回调函数之外的变量

[英]Accessing a variable outside of a socket.io callback function

I don't have much experience with Javascript, Node.js or Socket.io. 我对Javascript,Node.js或Socket.io没有太多的经验。 I'm trying to create an app with a jQuery pagination plugin, http://beneverard.github.com/jqPagination/ , that queries a mysql database (with keywords that the user enters into a textbox), gets the total number of results back along with the first 15 results for the first page (using LIMIT) then uses the total number of results to determine how many pages will be needed. 我正在尝试使用jQuery分页插件http://beneverard.github.com/jqPagination/创建一个应用程序,该程序查询mysql数据库(使用用户在文本框中输入的关键字),获取结果总数返回以及第一页的前15个结果(使用LIMIT),然后使用结果总数来确定需要多少页。

Here is the code snippet (client-side javascript for setting the maximum number of pages for pagination) that I can't get to work: 这是我无法使用的代码段(用于设置分页的最大页面数的客户端javascript):

var maxpagenumber;

socket.on('resultsnumber', function(count)
{
    maxpagenumber = ~ ~(this.count / 15) + 1;
});

$('.pagination').jqPagination({
    max_page: maxpagenumber,
    paged: function(page)
    {
        ...
    }
});

I looked at the variable maxpagenumber in Chrome and noticed that it is undefined. 我在Chrome中查看了变量maxpagenumber,发现它是未定义的。 The query for the first 15 results works it's just that the pagination only shows one maximum page. 前15个结果的查询有效,只是分页仅显示一个最大页面。

As I was looking around online for help I found a question on accessing a variable outside an asynchronous callback function: Using variable outside of ajax callback function . 当我在网上四处寻找帮助时,我发现了一个有关在异步回调函数外部访问变量的问题: 在ajax回调函数外部使用变量 I used the idea from the marked answer and applied it to my code: 我使用标记答案中的想法,并将其应用于我的代码:

var setPagination = function(maxpagenum)
{
    $('.pagination').jqPagination({
        max_page: maxpagenum,
        paged: function(page)
        {
            ...
        }
    });
}

socket.on('databasenumber', function(count)
{
    var maxpagenumber = ~ ~(this.count / 15) + 1;
    setPagination(maxpagenumber);
});

This gives the same result as the previous code snippet. 这给出的结果与先前的代码段相同。 The only working option I have so far is this: 到目前为止,我唯一可行的选择是:

socket.on('databasenumber', function(count)
{
    var maxpagenumber = ~ ~(this.count / 15) + 1;
    $('.pagination').jqPagination({
        max_page: maxpagenumber,
        paged: function(page)
        {
            ...
        }
    });
});

This works perfectly until the user enters new keywords into a textbox to search again. 直到用户在文本框中输入新的关键字以再次搜索之前,此方法均能完美运行。 Once the user does a second search many bugs arise. 一旦用户进行第二次搜索,就会出现许多错误。 Anyone have any ideas on how to make this work? 有人对如何进行这项工作有任何想法吗? Should I show more code to explain my problem? 我应该显示更多代码来解释我的问题吗? Maybe I'm just not understanding this snippet of Javascript properly? 也许我只是不正确理解这个Java代码片段? Any help or advise would be much appreciated. 任何帮助或建议,将不胜感激。

The problem seems to be that you're only setting the maxpagenumber once. 问题似乎是您只设置了一次maxpagenumber。 You really need to set that each time your dataset changes. 您确实需要在每次数据集更改时进行设置。 I think some combination of your second set reworked will be best, but I'm not 100% familiar with the jqPagination control, so that is probably where things aren't working out. 我认为重做第二组的某种组合是最好的,但是我对jqPagination控件不是100%熟悉,因此可能无法解决问题。 You may need to feed it a new maxPageNum via some API call? 您可能需要通过一些API调用为其提供新的maxPageNum?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM