Is there a way to prevent the setInterval function from running when the user unselects the div they originally selected? I've tried using clearInterval but it's not working. Also, utilizing the cvalue from this doesn't seem to update within the code. Any suggestions?
$(document).on("click",".profTemp", function()
{
var pollingUser;
if(!$(this).hasClass('selected'))
{
var chosenUser;
var previous = null;
var current = null;
var assigned = false;
chosenUser = $(this).children('span').text();
chosenUser = chosenUser.trim();
console.log(chosenUser);
$(this).toggleClass('selected');
pollingUser = setInterval(function()
{
console.log(chosenUser);
$.ajax({
url: '/getJSON',
dataType: 'json',
success: function(response) {
console.log(response + "84897348");
obj = JSON.parse(response);
console.log(chosenUser + "inside success");
var changeTo;
var deviceID;
console.log(current + "current");
for(var i = 0; i < obj.clients.length; i++)
{
if((chosenUser == obj.clients[i].userName) && (assigned == false))
{
console.log(chosenUser + " inside if");
current = JSON.stringify(obj.clients[i]);
changeTo = obj.clients[i].uri;
device = obj.clients[i].deviceID
assigned = true;
}
}
assigned = false;
console.log(current + " current");
console.log(previous + "previous");
if(previous !== current)
{
console.log("ARE WE INSIDE HERE");
$.ajax({
url: 'https://api.spotify.com/v1/me/player/play',
contentType: 'application/json',
type: 'PUT',
dataType: 'json',
headers: {
'Authorization': 'Bearer ' + access_token
},
data: JSON.stringify({"uris":[changeTo], "device_id":device, "offset": { "uri":changeTo} }),
success: function(response) {
console.log(response);
}
});
}
}
});
previous = current;
console.log(chosenUser);
}, 2000);
}
else if($(this).hasClass('selected'))
{
$(this).toggleClass('selected');
console.log("not selected");
clearInterval(pollingUser);
}
});
`