I've got a function that checks if a list item (setting) is true and then changes the setting which it does correctly but doesn't execute the second ExecuteQueryAsync functions. In debugging it jumps out of the code after that function/line and never reaches the OnQuerySuccess/Failure Here's what I have:
<html>
<head>
<script type="text/JavaScript">
<!--
function loopScript(){
setInterval(checkSetting, 5000);
}
function checkSetting() {
var clientContext = SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('Biz.WebPartSettings');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<Query> \
<Where> \
<Eq> \
<FieldRef Name='Title' /> \
<Value Type='Text'>PS-ImageSliderRefresh-ImageSlider</Value> \
</Eq> \
</Where> \
</Query>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
//console.log(oListItem.get_fieldValues());
if(oListItem.get_item('Setting_x0020_Value') == 'Yes'){
oListItem.set_item('Setting_x0020_Value', 'No');
console.log(oListItem.get_fieldValues());
oListItem.update();
clientContext.executeQueryAsync(Function.createDelegate(
this, this.onQuerySuccess), Function.createDelegate(this, this.onQueryFailure));
function onQuerySuccess(sender, args){
alert('Setting changed');
location.reload(true);
}
function onQueryFailure(sender, args) {
alert('Request Failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
}
}
function onQueryFailed(sender, args) {
alert('Request Failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
// -->
</script>
</head>
<body onload="JavaScript:loopScript();">
</body>
</html>