将循环变量传递给setTimeout函数

时间:2015-08-20 02:54:28

标签: javascript jquery loops for-loop settimeout

我的代码如下所示:

library(leaps)  
#Plotting function  plot.regsubsets2 <- 
    function (x, labels = obj$xnames, main = NULL, scale = c("bic", 
                "Cp", "adjr2", "r2"), col = gray(seq(0, 0.9, length = 10)), 
                ...) 
            {
                obj <- x
                lsum <- summary(obj)
                par(mar = c(7, 5, 6, 3) + 0.1)
                nmodels <- length(lsum$rsq)
                np <- obj$np
                propscale <- FALSE
                sscale <- pmatch(scale[1], c("bic", "Cp", "adjr2", "r2"), 
                    nomatch = 0)
                if (sscale == 0) 
                    stop(paste("Unrecognised scale=", scale))
                if (propscale) 
                    stop(paste("Proportional scaling only for probabilities"))
                yscale <- switch(sscale, lsum$bic, lsum$cp, lsum$adjr2, lsum$rsq)
                up <- switch(sscale, -1, -1, 1, 1)
                index <- order(yscale * up)
                colorscale <- switch(sscale, yscale, yscale, -log(pmax(yscale, 
                    1e-04)), -log(pmax(yscale, 1e-04)))
                image(z = t(ifelse(lsum$which[index, ], colorscale[index], 
                    NA + max(colorscale) * 1.5)), xaxt = "n", yaxt = "n", 
                    x = (1:np), y = 1:nmodels, xlab = "", ylab = scale[1], 
                    col = col)
                laspar <- par("las")
                on.exit(par(las = laspar))
                par(las = 2)
                axis(1, at = 1:np, labels = labels, ...) # I modified this line
                axis(2, at = 1:nmodels, labels = signif(yscale[index], 2))
                if (!is.null(main)) 
                    title(main = main)
                box()
                invisible(NULL)
            }

#Leap 
leaps = regsubets(y~x1+x2+x2, data=mydf, nbest=10) 
summary(leaps) 
plot.regsubsets2(leaps, scale='r2')

当我运行它时,我收到一条错误,说“$(document).ready(function() { $("#sendButton").click(function () { for (var i = 0; i < 119; i++) { setTimeout(function (i) { proName = proDatabase[i][0]; proChamp = proDatabase[i][1]; ... ”...意味着该setTimeout函数范围内的i值未定义。我正在尝试将循环中的值传递给setTimeout函数,以便循环每秒执行一次(它当前正在工作),这样我就不会太快地对服务器进行太多的AJAX调用。

尽管尝试通过(i),它仍然注册为未定义。

关于这个的任何想法?

谢谢!

0 个答案:

没有答案