需要有关 JS 循环的帮助

发布于 2024-08-26 05:11:00 字数 1210 浏览 4 评论 0原文

我有这样的函数,它添加了一个 droppables 网格:

function AddClassroomDrops(grid, weeks, days, times) {
    for(week = 1; week <= weeks; week++) {
        for (day = 1; day <= days; day++) {
            for (time = 1; time <= times; time++ ) {
                Droppables.add('container_grid'+ grid + '_week' + week + '_day' + day + '_time' + time, {
                    accept: 'pair',
                    hoverclass : 'hovered_receiver',
                    onDrop: function(pair, receiver) {
                        new Ajax.Request(
                          '/pairs/'+pair.id+'/update_on_drop', {
                            method : 'put',
                            parameters : {
                              classroom : grid,
                              week : week,
                              day : day,
                              time : time,
                              container : receiver.id
                            }
                          }
                        );
                      }
                });
            }
        }
     }
}

问题是 Ajax.Request 的参数(周,日,时间)始终等于周 + 1,次 + 1,天 + 1。但它们必须根据循环。哦,是的 - Droppables 来自 script.aculo.us 框架。

I have such function, that adds a grid of droppables:

function AddClassroomDrops(grid, weeks, days, times) {
    for(week = 1; week <= weeks; week++) {
        for (day = 1; day <= days; day++) {
            for (time = 1; time <= times; time++ ) {
                Droppables.add('container_grid'+ grid + '_week' + week + '_day' + day + '_time' + time, {
                    accept: 'pair',
                    hoverclass : 'hovered_receiver',
                    onDrop: function(pair, receiver) {
                        new Ajax.Request(
                          '/pairs/'+pair.id+'/update_on_drop', {
                            method : 'put',
                            parameters : {
                              classroom : grid,
                              week : week,
                              day : day,
                              time : time,
                              container : receiver.id
                            }
                          }
                        );
                      }
                });
            }
        }
     }
}

The problem is that params of Ajax.Request (week, day, time) are always equal to weeks + 1, times + 1, days + 1. But they must vary according to the cycle. Oh, yes - Droppables is from script.aculo.us framework.

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

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

发布评论

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

评论(2

戒ㄋ 2024-09-02 05:11:00

问题出在你对闭包的理解上。封闭函数中的局部变量周、日等的值将是 AddClassroomDrops 执行完成时的最后一个值。避免这种情况的典型方法是返回一个函数并将局部变量传递给另一个函数。例如:

function enclosing()
{
   for(var i = 0; i < 10; i++)
   {
       var f = function(j) { return function closureFunc() { // use j here }; }(i);
       // here you can do Droppables.add(f);
   }
}

The problem is with your understanding of closures. The value of week, day, etc. which are local variables in the enclosing function will be the last value at the time of AddClassroomDrops' completion of execution. The typical way to avoid this is returning a function and passing the local variable to yet another function. For example:

function enclosing()
{
   for(var i = 0; i < 10; i++)
   {
       var f = function(j) { return function closureFunc() { // use j here }; }(i);
       // here you can do Droppables.add(f);
   }
}
平生欢 2024-09-02 05:11:00

这在萨满舞蹈之后起作用:

function AddClassroomDrops(grid, weeks, days, times) {
    for(week = 1; week <= weeks; week++) {
        for (day = 1; day <= days; day++) {
            for (time = 1; time <= times; time++ ) {
                var drop = function(week, day, time) {
                    Droppables.add('container_grid'+ grid + '_week' + week + '_day' + day + '_time' + time, {
                        accept: 'pair',
                        hoverclass : 'hovered_receiver',
                        onDrop: function(pair, receiver) {
                            new Ajax.Request(
                              '/pairs/'+pair.id+'/update_on_drop', {
                                method : 'put',
                                parameters : {
                                  classroom : grid,
                                  week : week,
                                  day : day,
                                  time : time,
                                  container : receiver.id
                                }
                              }
                            );
                          }
                    });
                 }
              drop(week, day, time);
            }
        }
    }
}

This works after shaman dances:

function AddClassroomDrops(grid, weeks, days, times) {
    for(week = 1; week <= weeks; week++) {
        for (day = 1; day <= days; day++) {
            for (time = 1; time <= times; time++ ) {
                var drop = function(week, day, time) {
                    Droppables.add('container_grid'+ grid + '_week' + week + '_day' + day + '_time' + time, {
                        accept: 'pair',
                        hoverclass : 'hovered_receiver',
                        onDrop: function(pair, receiver) {
                            new Ajax.Request(
                              '/pairs/'+pair.id+'/update_on_drop', {
                                method : 'put',
                                parameters : {
                                  classroom : grid,
                                  week : week,
                                  day : day,
                                  time : time,
                                  container : receiver.id
                                }
                              }
                            );
                          }
                    });
                 }
              drop(week, day, time);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文