0

想要在找到它后将一个简单的整数传递给函数。代码如下:

$("#play").click(function() {
    var elementNumber;
    var properElement;
    for (var i = 0; i < playlistLength; i++) {
        if (listOfElements[i].type == 'video' && listOfElements[i].position == 0) {
            elementNumber = i;
        }
    };
    document.getElementById(listOfElements[elementNumber].name).play();
    properElement = listOfElements[elementNumber];
    console.log(properElement); // gives out proper element;
    setInterval("galleryElement.draw(properElement.name, properElement.type, properElement.position)", 33); // Cannot read property 'name' of undefined 
    return false;
})​

我得到一个错误Cannot read property "name" of undefined?我怎样才能在那里传递论点?

提前致谢!

4

3 回答 3

0
$("#play").click(function() {
    var elementNumber;
    var properElement;
    for (var i = 0; i < playlistLength; i++) {
        if (listOfElements[i].type == 'video' && listOfElements[i].position == 0) {
            elementNumber = i;
        }
    };
    document.getElementById(listOfElements[elementNumber].name).play();
    properElement = listOfElements[elementNumber];
    console.log(properElement); // gives out proper element;
    setInterval(function() {
        galleryElement.draw(properElement.name, properElement.type, properElement.position)
    }, 33);
    return false;
})

或(在 IE 中不起作用)

$("#play").click(function() {
    var elementNumber;
    var properElement;
    for (var i = 0; i < playlistLength; i++) {
        if (listOfElements[i].type == 'video' && listOfElements[i].position == 0) {
            elementNumber = i;
        }
    };
    document.getElementById(listOfElements[elementNumber].name).play();
    properElement = listOfElements[elementNumber];
    console.log(properElement); // gives out proper element;
    setInterval(galleryElement.draw, 33, properElement.name, properElement.type, properElement.position);
    return false;
})
于 2012-09-18T12:09:48.040 回答
0

尝试

setInterval(function(){
  galleryElement.draw(properElement.name, properElement.type, properElement.position)
}, 33);

您需要放入properElement要执行的函数的范围内。

于 2012-09-18T12:10:08.977 回答
0

如果您将字符串传递给您,setInterval()您将失去properElement 的范围。您必须直接传递函数调用(不带引号):

setInterval(function(){
  galleryElement.draw(properElement.name, properElement.type, properElement.position)
}, 33);

通常不建议将字符串传递给,setInterval/Timeout因为它需要通过eval. 始终传递函数或对函数的引用。

于 2012-09-18T12:14:55.340 回答