setTimeout in app.js not working

Hello Team,

I am trying to use the setTimeout function in app.js file, but it is not working.

Can you please let me know will it work or not?

total Code Snippet:

let ticketID;

document.onreadystatechange = function() {

if (document.readyState === ‘interactive’) renderApp();

function renderApp() {

var onInit = app.initialized();

onInit

  .then(function getClient(_client) {

    window.client = _client;

    client.events.on('app.activated', renderContactName);

    

    client.events.on("ticket.previousTicketClick", eventCallback);

    

    client.events.on("ticket.nextTicketClick", eventCallback);

  })

  .catch(handleErr);

}

};

var eventCallback = function (data) {

console.log(data.type + " event occurred");

let dbName = '${ticketID}End';

getDbDtails(client, dbName);

};

var eventCallback = function (data) {

console.log(data.type + " event occurred");

let dbName = '${ticketID}End';

getDbDtails(client, dbName);

};

function renderContactName() {

console.log(“WWWWWWWWWWWWWWWWW”);

setTimeout(

client.data.get("ticket").then (function(data) {

  console.log(data.ticket)

  ticketID = data.ticket.display_id;

  if (ticketID != undefined) {

    let dbName = `'${ticketID}Start'`;

    getDbDtails(client, dbName);

  }

},

function(error) {

  console.log(error);

}),30000);

}

function handleErr(err = ‘None’) {

console.error(Error occured. Details:, err);

}

function setDb (client, dbName) {

let today = new Date();

let time = today.getHours() + “:” + today.getMinutes();

console.log(time);

let ticketObj = {“currentTime”:time};

client.db.set( dbName, ticketObj).then (function() {

console.log("DB set !!!");

console.log("dbName.includes('End') ", dbName.includes('End'));

if (dbName.includes('End')) {

  updateField(client, dbName);

}

},function(error) {

// failure operation

console.log(error)

});

}

function deleteDb(client, dbName) {

client.db.delete(dbName).then (function() {

  console.log("DB deleted !!!!!!!!");

  setDb (client, dbName);

},

function(error) {

  console.log(error);

  // failure operation

});

}

function getDbDtails(client, dbName) {

client.db.get(dbName).then (function() {

  console.log("Got the Data !!!!!");

  deleteDb(client, dbName);

},

function(error) {

  console.log(error)

  setDb (client, dbName);

});

}

function updateField(client, dbName) {

console.log(“in updateField function !!!”);

let currentTicketID = dbName.split(“E”)[0];

console.log("currentTicketID ", currentTicketID);

let startDbName = '${ticketID}Start';

client.db.get(startDbName).then (function(startData) {

console.log("Got the Data !!!!!", startData);

// deleteDb(client, dbName);

let startTime = startData.currentTime;

client.db.get(dbName).then (function(endTimeData) {

  console.log("Got the Data !!!!!", endTimeData);

  // deleteDb(client, dbName);

  let endTime = endTimeData.currentTime;

  var hours;

  var minutes;

  var splitted1 = startTime.split(":");

  var splitted2 = endTime.split(":");

  let startTimeHrs = splitted1[0];

  let endTimeHrs = splitted2[0];

  let startTimeMins = splitted1[1];

  let endTimeMins = splitted2[1];

  if (endTimeMins == startTimeMins) {

    minutes = 0;

  } else if (endTimeMins > startTimeMins) {

    minutes = parseInt(endTimeMins) - parseInt(startTimeMins);

  } else {

    minutes = parseInt(startTimeMins) - parseInt(endTimeMins);

  }

  if (startTimeHrs == endTimeHrs) {

    hours = 0;

  } else if (startTimeHrs < endTimeHrs) {

    hours = parseInt(endTimeHrs) - parseInt(startTimeHrs);

  } else {

    let hours1 = parseInt(startTimeHrs) - parseInt(endTimeHrs);

    hours = 24 - parseInt(hours1);

  }

  console.log(hours, minutes);

  let finalTime = (parseInt(hours) * 60) + parseInt(minutes);

  console.log("finalTime ", finalTime);

  client.interface.trigger("setValue", {id: "matter_number_172326", value: finalTime})

},

function(error) {

  console.log(error)

});

},

function(error) {

console.log(error)

});

}

Regards,
Akhil S Kulkarni

Hello Team,

Please respond to the topic as this is a priority request and need to complete it by Monday.

Regards,
Akhil S K

Hi @Akhil_Kulkarni

Can you please let me know what error you are facing? Is the setTimeout function throwing any error or setTimeout function is not even executed?

Regards,
Mughela Chandresh

Hello @Mughela_Chandresh ,

I am not getting any error in setTimeout function, but it is not working as per it’s reputation.

As you can see I am setting a 30 secs in the setTimeout function so that the next function should run after 30 secs but it is not happening.

Can you please look into this or let me know if I am missing something here?

Regards,
Akhil S K

Hi @Akhil_Kulkarni

Can you please reduce the timeout to like 3 seconds and test it locally. I have tested your code locally and it works for me.

1 Like

Hello @Mughela_Chandresh ,

Can we make a time as 30 secs as this is the customer’s requirement?

Regards,
Akhil S K

@Akhil_Kulkarni ,

I think your code has some syntax issues, please find the below difference.

wrong way: :point_down:
setTimeout(console.log(‘something’), 5000);

right way: :point_down:
setTimeout(function () {console.log(‘updated’)},5000);

hope it helps :slight_smile:

Thanks