Hi all,
I have a small silly issue which i'm embarrassed I cant solve...

I have a function which loops every second to see if a status has changed.
When the status changes, I want it to call a phonegap 'beep' notification.
The issue is it keeps calling the beep sound every second due to the constant iteration of the function.

I thought the best solution was to declare a global variable to 'false' and change it to true once first iteration is passed, but it didnt work.
Can anybody guide me on this?

Thanks all.

function checkResponseStatus(){

$.getJSON("requestrespond.php",  

function(data)
{        
        //variables from output object
        var id =    parseInt((data.id),10);
        var respond_date =  (data.respond_date);
        var respond_status = parseInt((data.respond_status),10);
        var respond_time = (data.respond_time);


        if (respond_status == 1) {        //request has been made

            document.getElementById("responseStatus").disabled = false;
            document.getElementById("responseStatus").innerHTML = "Request Sent on "+ respond_date + " <br /> Time: " + respond_time + "<br /> " + "Click Here To Respond";


            navigator.notification.beep(1);


        }   


       }

        else{
        document.getElementById("responseStatus").disabled = true;
        }

}); 

}

Recommended Answers

All 3 Replies

The issue is it keeps calling the beep sound every second due to the constant iteration of the function.

Isnt it because the response_status coming back is always set to 1?

Sorry, I'm not clear on the process here based on what you are trying to do. From what I understand based on your description, you are calling this function every second and you are doing an ajax call to the PHP page, depending on the value of data.respond_time, you are taking a specific course of action.

I apologize if I was not clear JorgeM.

So the checkResponseStatus function gets called every second.
Within the function is 'navigator.notification.beep(1);' which I only want to call once and not every time the checkResponseStatus function is executed.

As a note: the response_status variable has nothing to do with issue.

Hope this was clearer.

Thanks again

var beeped = false;
function checkResponseStatus(){

    $.getJSON("requestrespond.php",  

    function(data)
    {   
        ...
        ...
        ...
        if (respond_status == 1) {  //request has been made
            document.getElementById("responseStatus").disabled = false;
            document.getElementById("responseStatus").innerHTML = "Request Sent on "+ respond_date + " <br /> Time: " + respond_time + "<br /> " + "Click Here To Respond";

            if(!beeped) {
                navigator.notification.beep(1);
                beeped = true;
            }   
        }

        ...
        ...
        ...

    }

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.