0

I'm having a problem in interpreting this code, I'm developing a seat arrangement Jquery Script. Please help me in interpreting this jquery code

setInterval(function() {
$.ajax({
    type     : 'get',
    url      : '/bookings/get/100',
    dataType : 'json',
    success  : function(response) {
        //iterate through all bookings for our event 
        $.each(response.bookings, function(index, booking) {
            //find seat by id and set its status to unavailable
            sc.status(booking.seat_id, 'unavailable');
        });
    }
});}, 10000); //every 10 seconds
2
  • setInterval call $.ajax every 10sec; $.ajax send a request to {your_domain}/bookings/get/100; If this url is responding valid JSON the success function is called where response is the JSON object; $.each run a function on each item in response.bookings; sc.status is some custom function that is called with booking.seat_id Commented Jan 13, 2016 at 15:21
  • Thank you Philipp, I'm new to Jquery, there is this part I can not really understand. $.each(response.bookings, function(index, booking) { //find seat by id and set its status to unavailable sc.status(booking.seat_id, 'unavailable'); would you kindly show me how the JSON object would look like?. I will appreciate your responce Commented Jan 13, 2016 at 16:26

1 Answer 1

1

At least response.bookings can be either an object or an array. if you what to find you what is you case just run console.log(response.bookings); and check the debugger console.

As an object it could look like (i added some fictional fields):

var response = {
   status: 'ok',
   bookings: {
   //index: booking
       a: {seat_id: 1, user_id: 4, paid: true},
       b: {seat_id: 2, user_id: 3, paid: false},
       c: {seat_id: 3, user_id: 2, paid: true},
       d: {seat_id: 4, user_id: 1, paid: false}
   }  
};

//$.each means that function is called of each entry of response.bookings
 $.each(response.bookings, function(index, booking) {
     // frist run index='a', booking={seat_id: 1, user_id: 1, paid: true} so booking.seat_id=1
   //second run index='b', booking={seat_id: 2, user_id: 2, paid: false} so booking.seat_id=2
   // third run index='c', booking={seat_id: 3, user_id: 3, paid: true} so booking.seat_id=3
   // fours run index='d', booking={seat_id: 4, user_id: 4, paid: false} so booking.seat_id=4
   sc.status(booking.seat_id, 'unavailable');
   // you can check the values by using console again:
   // console.log(index);
   // console.log(booking);
 });

As an array it could look like (i added some fictional fields):

var response = {
    status: 'ok',
  bookings: [
  // array of booking object (index incrementel starting from 0)
    {seat_id: 1, user_id: 4, paid: true},
    {seat_id: 2, user_id: 3, paid: false},
    {seat_id: 3, user_id: 2, paid: true},
    {seat_id: 4, user_id: 1, paid: false}
  ]  
};

//$.each means that function is called of each entry of response.bookings
 $.each(response.bookings, function(index, booking) {
     // frist run index=0, booking={seat_id: 1, user_id: 1, paid: true} so booking.seat_id=1
   //second run index=1, booking={seat_id: 2, user_id: 2, paid: false} so booking.seat_id=2
   // third run index=2, booking={seat_id: 3, user_id: 3, paid: true} so booking.seat_id=3
   // fours run index=3, booking={seat_id: 4, user_id: 4, paid: false} so booking.seat_id=4
   sc.status(booking.seat_id, 'unavailable');
   // you can check the values by using console again:
   // console.log(index);
   // console.log(booking);
 });

in general is $.each only a jquery version of an for loop. Here how you can do it using plain javascript (maybe helps you to unterstand ):

// response.bookings is an object
 var booking;
 for (index in response.bookings){
     booking = response.bookings[index];
   sc.status(booking.seat_id, 'unavailable');
   //console.log(booking);
 }

 // response.bookings is an array
 var booking;
 for (var index = 0; index < response.bookings,length; index++){
     booking = response.bookings[index];
   sc.status(booking.seat_id, 'unavailable');
   //console.log(booking);
 }
Sign up to request clarification or add additional context in comments.

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.