简体   繁体   English

Jquery / Javascript仅在CSS更改后运行一次自定义函数

[英]Jquery/Javascript only running a custom function once after CSS changes

I'm using Jquery to draw lines between the cells in a web-based spreadsheet application. 我正在使用Jquery在基于Web的电子表格应用程序中的单元格之间绘制线条。 I accomplish this with a mixture of background-image , background-position and background-repeat . 我用背景图像背景位置背景重复的混合来实现这一点。 The entire system is working very nicely, and allows me to map between different cells in the application. 整个系统运行良好,允许我在应用程序中的不同单元格之间进行映射。

However, I'm having some trouble with my Jquery/Javascript code. 但是,我在使用Jquery / Javascript代码时遇到了一些麻烦。

function draw_line(start_row, start_col, finish_row, finish_col){
    //Change CSS background properties
}

function loadData(){
    for (i = 0; i < values; i++){
        //Add element to spreadsheet - change CSS properties.

        //Draw line between the two cells
        draw_line(start_row, start_column, line, column);
    }
}

loadData();

The problem is that although the for loop should run several times for all the elements involved, it will actually only run once . 问题是虽然for循环应该对所涉及的所有元素运行几次,但它实际上只运行一次 However, if I comment out the draw_line function, the for loop executes the correct number of times, and all of the elements get placed on the spreadsheet. 但是,如果我注释掉draw_line函数,for循环会执行正确的次数,并且所有元素都会放在电子表格中。

I've also tried running setTimeout, but that didn't help. 我也试过运行setTimeout,但这没有帮助。 Anyone every experienced this sort of behavior before? 以前每个人都经历过这种行为吗? (Just for reference, I'm running JQuery v1.6.4, on FF) (仅供参考,我在FF上运行JQuery v1.6.4)

Hope someone can help. 希望有人能提供帮助。 Thanks! 谢谢!

I have no idea if this is actually causing your problem, but it's bad and risky code so you should fix it. 我不知道这是否真的导致你的问题,但它是坏的和有风险的代码所以你应该修复它。 This line of your code: 这行代码:

for (i = 0; i < values; i++)

is using an implicitly declared global variable as i . 正在使用隐式声明的全局变量作为i If any other code you are running is also doing that, then the value of i will get trounced and wreck your for loop. 如果你正在运行的任何其他代码也正在这样做,那么i的值将被破坏并破坏你的for循环。

Change that line to this to make i a local variable so nothing else can trounce it: 将该行更改为此选项以使i成为局部变量,因此没有其他任何内容可以破坏它:

for (var i = 0; i < values; i++)

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

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