在我为用户检索帐户之前,我需要调用一个函数来发布帐户ID.在该函数中,我需要检查检索到的帐户是否已存在于用户端,如果存在,则增加查询ID并重新运行该函数,直到检索到的帐户对用户来说是新的.
由于函数会调用自身,所以我想我正在做的是在我的生活中编写我的第一个递归函数.
我试过了:
<script>
function updateAcc(maxVal) {
$.post("accountUpdate.php", {maxVal:maxVal}, function(result){
// get the retrieved account ID
var result=$(result);
pa.replaceWith(result);
var resultid=result.attr("data-topic-id");
var testlength=$('*[data-topic-id="'+resultid+'"]').length;
// check if retrived account already exists on user's page
// if exists alraedy, increase the query ID, run the function again, retrieved new account ID,until there's no same accounts on users' side
if(testlength>1) {
maxVal++;
updateAcc();
}
// alert(resultid);
})
}
// envoke the function
updateAcc(maxVal)
</script>
Run Code Online (Sandbox Code Playgroud)