2

The function StartScan() is called trough a button-click and addDevices() creates the output dynamically. I want this output every 2 or 5 seconds automatically updated.

In the console I see that the function StartScan() reloads automatically, so the values are there and I just have put the addDevice() function in a loop.

Where am I wrong?

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);    
}
  • If you're using jQuery please add the tag to your question. – j08691 Jun 05 '15 at 14:12
  • What is `format()`? Are you sure it returns the jQuery object? – George Jun 05 '15 at 14:12
  • 4
    Note: [`setTimeout()`](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout) expects a function to call along with the timeout. It isn't an equivalent to `sleep()` functions that pause the current thread, delaying following code. It just delays calling the function that's provided to it. – Jonathan Lonowski Jun 05 '15 at 14:14
  • 1
    `setTimeout(50000)` is totally useless. Javascript is asynchronous, it will launch the timeout and keep going with the code in the meantime, while the timeout counts. – Jeremy Thille Jun 05 '15 at 14:15
  • You're probably looking for something like: http://stackoverflow.com/questions/8663246/javascript-timer-loop – Cerbrus Jun 05 '15 at 14:17
  • @George Yes I'm sure. format() ist just a simple format method to change the values to strings – SaveOurPlanet Jun 05 '15 at 14:20
  • @Jeremy Thille apart from my wrong use of setTimeout() the loop is still not working. Could there be another reason? – SaveOurPlanet Jun 05 '15 at 14:30
  • Did you see my answer below? – Jeremy Thille Jun 05 '15 at 14:36
  • @Cerbrus yea, kind of. I already edited my addDevice function and changed setTimeout to setInterval and tried different variations, but still not working. – SaveOurPlanet Jun 08 '15 at 14:53

1 Answers1

0

I believe you want :

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

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

This will launch the addDevice function every 2 seconds.

Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • Define "not working". It's very vague. Do you have errors in the console? What if you console.log stuff? How do you know if the function is called or not? etc. – Jeremy Thille Jun 05 '15 at 19:08
  • There are no error in the console.log, quiet the contrary trough the console.log("Start Scan : " + JSON.stringify(paramsObj)) I see on the console exactly that what I want to see on the screen. The addDevice function is called once, but I want it to repeat, like the startscan function does. – SaveOurPlanet Jun 05 '15 at 19:10
  • I have added a variant to my code to launch the function with arguments, try this? – Jeremy Thille Jun 05 '15 at 19:45
  • You don't have to be sorry. But as I said earlier, "It's not working" isn't helpful at all. It just indicates me that it's not working, and does not give a clue about how to debug this and make it work. – Jeremy Thille Jun 08 '15 at 13:13
  • nothing changed - it's like it ignores the setInterval(), but there are still no errors and the App works like before. – SaveOurPlanet Jun 08 '15 at 14:56