简体   繁体   English

在JS循环方面需要帮助

[英]Need help with cycle in JS

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. 问题在于Ajax.Request的参数(周,日,时间)始终等于周+ 1,时间+ 1,天+1。但是它们必须根据周期而变化。 Oh, yes - Droppables is from script.aculo.us framework. 哦,是的-Droppables来自script.aculo.us框架。

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. 封闭函数中作为局部变量的星期,天等的值将是AddClassroomDrops完成执行时的最后一个值。 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);
   }
}

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);
            }
        }
    }
}

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

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