带append()的简单循环不起作用

时间:2015-06-05 14:11:17

标签: javascript jquery loops jquery-mobile

通过按钮单击调用函数StartScan()addDevices()动态创建输出。我想每2或5秒自动更新此输出。

在控制台中,我看到函数StartScan()自动重新加载,因此值就在那里,我只是将addDevice()函数放在循环中。

我哪里错了?

function startScan()
{
    var paramsObj = {serviceUuids:[]};

    console.log("Start Scan : " + JSON.stringify(paramsObj));

    bluetoothle.startScan(startScanSuccess, startScanError, paramsObj);

    return false;
}

function startScanSuccess(obj)
{
    console.log("Start Scan Success : " + JSON.stringify(obj));     

    if (obj.status == "scanResult")
    {
        console.log("Scan Result");

        addDevice(obj.address, obj.name, obj.rssi);
    }
    else if (obj.status == "scanStarted")
    {
        console.log("Scan Started");
    }
    else
    {
        console.log("Unexpected Start Scan Status");
    }
}

function addDevice(address, name, rssi)
{
        var $devices = $(".devices");

        var $check = $devices.find("li[data-address='{0}']".format(address));

        if ($check.length > 0)
        {
            return;
        }
        var template = $("#device").text().format(address, name, rssi);

        $devices.append(template);
        $devices.trigger("create");

        window.setTimeout(50000);
        console.log("Loop: " + i);

        if (rssi < -100){   
            alert(name + " lost proximity");
        }

        setInterval( function(){ addDevice(adress, name, rssi);}, 2000);    
}

1 个答案:

答案 0 :(得分:0)

我相信你想要:

function addDevice(address, name, rssi)
{
        var $devices = $(".devices");
.....   
}

setInterval( addDevice, 2000); // without arguments
setInterval( function(){ addDevice(address, name, rssi) }, 2000); // with arguments

这将每2秒启动一次addDevice功能。