On my website I have a javascript function that does an AJAX call to get account's information then opens a modal where you can see and edit the information. An AJAX call is used to change the detail in the database you select and then refreshes the original function to re-open the modal to refresh the information there. It seems however, that occasionally (1/2+ times done) the details don't update fast enough in the DB before the functions that collects the details to display is run again.
I want to try delaying when the second function is ran to give a better chance for the details being updated before being fetched again, however I am unsure on how to do this. I've tried various things, however the one that seems the most popular, as shown below, does not work. Do you have any suggestions how I can fix it so the code pauses for x time before continuing?
function ChangeRank(StrUsername, StrPage)
{
var StrRank = $("#sltRank option:selected").val();
//Updates info in database
ModeratorEditAccount(StrUsername, StrRank, 'Rank', StrPage);
//Displays info again
setTimeout(ModeratorActions(StrUsername, StrPage), 30000);
}
function ModeratorEditAccount(StrUsername, Value, StrDetail, StrPage)
{
$.post('http://thomas-smyth.co.uk/functions/php/fncmoderatoreditaccount.php',
{
StrUsername: StrUsername,
Value: Value,
StrDetail: StrDetail
}, function(data)
{
if (data == 0)
{
$("#mdlGeneral > div").modal("hide");
if (StrSearchType == "Basic")
{
UserBasicSearch(StrPage);
}
else
{
UserAdvanceSearch(StrPage);
}
}
else if (data == 10)
{
window.location.href = "http://thomas-smyth.co.uk/login.php";
}
});
}
function ModeratorActions(StrUsername, StrPage)
{
$.post('http://thomas-smyth.co.uk/functions/php/fncgetaccountdetails.php',
{
StrUsername: StrUsername
}, function(data)
{
var returnValue = JSON.parse(data);
if (data == 5)
{}
else
{
if (returnValue["StrGender"] == "M")
{
StrGender = "Male";
}
else
{
StrGender = "Female";
}
$("#mdlEditProfile").html('<div class="modal" tabindex="-1" role="dialog"><div class="modal-dialog" role="document"><div class="modal-content" style="border-radius: 25px;"><div class="box box-widget widget-user-2"><div class="widget-user-header bg-yellow"><div class="widget-user-image"><img class="img-circle" src="../dist/img/user2-160x160.jpg" alt="User Avatar"></div><h3 class="widget-user-username">' + returnValue['StrSurname'] + ', ' + returnValue['StrForename'] + ' (' + returnValue['StrUsername'] + ')</h3><h5 class="widget-user-desc">Member Since: ' + returnValue['DteRegDate'] + '</h5></div>\<div class="box-footer no-padding"><ul class="nav nav-stacked"><li><a><strong>Name: </strong>' + returnValue['StrSurname'] + ', ' + returnValue['StrForename'] + '<span class="pull-right badge bg-blue" style="cursor: pointer;" onclick="ChangeNameOpen(\'' + returnValue['StrUsername'] + '\', \'' + returnValue['StrSurname'] + '\', \'' + returnValue['StrForename'] + '\', \'' + StrPage + '\')"><i class="fa fa-fw fa-edit"></i> Edit</span></a></li><li><a><strong>Username: </strong>' + returnValue['StrUsername'] + '<span class="pull-right badge bg-blue" style="cursor: pointer;" onclick="ChangeUsernameOpen(\'' + returnValue['StrUsername'] + '\')"><i class="fa fa-fw fa-edit"></i> Edit</span></a></li><li><a><strong>Date of Birth: </strong>' + returnValue['DteDoB'] + '<span class="pull-right badge bg-blue" style="cursor: pointer;" onclick="ChangeDoBOpen(\'' + returnValue['StrUsername'] + '\', \'' + returnValue['DteDoB'] + '\')"><i class="fa fa-fw fa-edit"></i> Edit</span></a></li><li><a><strong>Gender: </strong>' + returnValue['StrGender'] + '<span class="pull-right badge bg-blue" style="cursor: pointer;" onclick="ChangeGenderOpen(\'' + returnValue['StrUsername'] + '\')"><i class="fa fa-fw fa-edit"></i> Edit</span></a></li><li><a><strong>Account Rank: </strong>' + returnValue['StrRank'] + '<span class="pull-right badge bg-blue" style="cursor: pointer;" onclick="ChangeRankOpen(\'' + returnValue['StrUsername'] + '\', \'' + returnValue['StrRank'] + '\', \'' + StrPage + '\')"><i class="fa fa-fw fa-edit"></i> Edit</span></a></li></ul></div></div></div></div></div>');
$("#mdlEditProfile > div").modal("show");
}
});
}