0

I'm having problems with this javascript code, second alert of (tracking_data) throws 'undefined' like if the variable was deleted or cleared and I don't see any part of the code doing that. I hope you can help me with this. Regards.

var track_id = '';      // Name/ID of the exercise
var watch_id = null;    // ID of the geolocation
var tracking_data = []; // Array containing GPS position objects

$("#startTracking_start").live('click', function () {

// Start tracking the User
watch_id = navigator.geolocation.watchPosition(

    // Success
    function (position) {
        tracking_data.push(position);
        alert(tracking_data);
    },

    // Error
    function (error) {
        console.log(error);
    },

    // Settings
        {frequency: 3000, enableHighAccuracy: true });

    // Tidy up the UI
    track_id = $("#track_id").val();

    $("#track_id").hide();

    $("#startTracking_status").html("Tracking workout: <strong>" + track_id + "</strong>");
});


$("#startTracking_stop").live('click', function(){
    alert(tracking_data);

// Stop tracking the user
navigator.geolocation.clearWatch(watch_id);

// Save the tracking data
window.localStorage.setItem(track_id, JSON.stringify(tracking_data));

// Reset watch_id and tracking_data 
var watch_id = null;
var tracking_data = null;

// Tidy up the UI
$("#track_id").val("").show();

$("#startTracking_status").html("Stopped tracking workout: <strong>" + track_id + "    </strong>");

});
2
  • did you try alerting position? it's probably undefined. Commented Jul 13, 2013 at 4:19
  • @kennypu the code is pushing data into the array, but when I try to read it from the other function it give me 'undefined' error. Commented Jul 13, 2013 at 4:24

2 Answers 2

1

Due to hoisting, all var declarations are moved to the top of it's enclosing function. In other words, you're overriding tracking_data inside your function.

Changing from var tracking_data = null; (inside your function) to tracking_data = null; will solve your problem.

The example below reproduces this behaviour:

var v1 = 'hello';
(function(){
  console.log(v1); //prints hello
  v1 = 'hi';
  console.log(v1); //prints hi
})()

.

var v2 = 'hello';
(function(){
  console.log(v2); //prints undefined. Due to hoisting the var v2 was overriden inside this scope. 
  var v2 = 'hi';
  console.log(v2); //prints hi
})()
Sign up to request clarification or add additional context in comments.

Comments

1

By using var tracking_data = null, you make tracking_data local to the click callback. It's not the same tracking_data from the outer scope. Due to JavaScript's variable and function hoisting, the variable declaration is done at the very beginning of the function, which will set tracking_data to undefined.

Get rid of var and on that line (and the line before it) and your code should work as intended.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.