$('.vote-btn').click(function (e) { e.preventDefault(); $('.vote-btn i').removeClass('fa').addClass('far'); $('i', $(this)).removeClass('far').addClass('fa'); let voteType; if ($(this).hasClass('upvote')) { voteType = 1; } else { voteType = -1; } const voteCount = $('.vote-container span'); voteCount.text(parseInt(voteCount.text()) + voteType); const container = $('body > section.container'); voteCount.parent().append(''); $('.vote-container .fa-sync').fadeIn(); $.ajax({ url: 'http://192.168.178.39/projects/addVote/index', method: 'POST', data: { id: $('.vote-container').data('id'), type: voteType }, success: function (result) { if (result['type'] === 'success') { voteCount.text(result['voteCount']); container.prepend(''); } else { voteCount.text(parseInt(voteCount.text()) - voteType); container.prepend('') } $('.vote-container .fa-sync').fadeOut(); $('#voteMsg').animate({ bottom: 10 }, 100) .delay(5000) .animate({ bottom: -80 }, 100); setTimeout(() => { $('#voteMsg').remove(); $('.vote-container .fa-sync').remove(); }, 6000); } }); });