Skip to main content
Cleanup, use `find` to avoid additional loops. updateCounter() doesn't need a dedicated function
Source Link
Blue
  • 23k
  • 1
  • 19
  • 28
$(document).ready(function() {
  $.getJSON("https://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
      var originalScore;
      console.log(data);
      $.each(data.items, function (i, v) {
          if (v.answer_id == 270116) {
              originalScore = v.score;
              return;
          }
      });

      $("#postCounter span").text(originalScore);

      var counter = 60;

      function updateCounter() {
          $("#counter span").text(counter);
          counter--;
          if (counter == 0) {
              counter = 60;
              $.getJSON("https://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
                  var newScore = 0;
                  $.each(data.items, function (i, v) {
                      if (v.answer_id == 270116) {
                          newScore = v.score;
                          return;
                      }
                  });
                  if (newScore > originalScore) {
                      $("#message").html("WOOHOO! You upvoted!<img src='https://i.imgur.com/B5aTato.gif' />");
                  } else if (newScore < originalScore) {
                      $("#message").text("Someone downvoted! :( Upvote!");
                  } else {
                      $("#message").text("Upvote to see the result! (nothing new)");
                  }
                  originalScore = newScore;
              });
          }
          setTimeout(function () {
              updateCounter();
          }, 1000);
      };

      updateCounter();
  });
});
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<div id="message">Upvote to see the result!</div>
<div id="counter">Time until next request: <span></span>
</div>
<div id="postCounter">This answer had <span></span> votes when you loaded the demo.</div>
$(document).ready(function() {
  $.getJSON("https://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
  
      var originalScore = data.items.find(v => v.answer_id == 270116).score;

      $("#postCounter span").text(originalScore);

      var counter = 60;

      function updateCounter() {
          $("#counter span").text(counter);
          counter--;
          if (counter == 0) {
              counter = 60;
              $.getJSON("https://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
                  var newScore = data.items.find(v => v.answer_id == 270116).score;
                  if (newScore > originalScore) {
                      $("#message").html("WOOHOO! You upvoted!<img src='https://i.imgur.com/B5aTato.gif' />");
                  } else if (newScore < originalScore) {
                      $("#message").text("Someone downvoted! :( Upvote!");
                  } else {
                      $("#message").text("Upvote to see the result! (nothing new)");
                  }
                  originalScore = newScore;
              });
          }
          setTimeout(updateCounter, 1000);
      };

      updateCounter();
  });
});
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<div id="message">Upvote to see the result!</div>
<div id="counter">Time until next request: <span></span>
</div>
<div id="postCounter">This answer had <span></span> votes when you loaded the demo.</div>
$(document).ready(function() {
  $.getJSON("https://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
      var originalScore;
      console.log(data);
      $.each(data.items, function (i, v) {
          if (v.answer_id == 270116) {
              originalScore = v.score;
              return;
          }
      });

      $("#postCounter span").text(originalScore);

      var counter = 60;

      function updateCounter() {
          $("#counter span").text(counter);
          counter--;
          if (counter == 0) {
              counter = 60;
              $.getJSON("https://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
                  var newScore = 0;
                  $.each(data.items, function (i, v) {
                      if (v.answer_id == 270116) {
                          newScore = v.score;
                          return;
                      }
                  });
                  if (newScore > originalScore) {
                      $("#message").html("WOOHOO! You upvoted!<img src='https://i.imgur.com/B5aTato.gif' />");
                  } else if (newScore < originalScore) {
                      $("#message").text("Someone downvoted! :( Upvote!");
                  } else {
                      $("#message").text("Upvote to see the result! (nothing new)");
                  }
                  originalScore = newScore;
              });
          }
          setTimeout(function () {
              updateCounter();
          }, 1000);
      };

      updateCounter();
  });
});
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<div id="message">Upvote to see the result!</div>
<div id="counter">Time until next request: <span></span>
</div>
<div id="postCounter">This answer had <span></span> votes when you loaded the demo.</div>
$(document).ready(function() {
  $.getJSON("https://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
  
      var originalScore = data.items.find(v => v.answer_id == 270116).score;

      $("#postCounter span").text(originalScore);

      var counter = 60;

      function updateCounter() {
          $("#counter span").text(counter);
          counter--;
          if (counter == 0) {
              counter = 60;
              $.getJSON("https://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
                  var newScore = data.items.find(v => v.answer_id == 270116).score;
                  if (newScore > originalScore) {
                      $("#message").html("WOOHOO! You upvoted!<img src='https://i.imgur.com/B5aTato.gif' />");
                  } else if (newScore < originalScore) {
                      $("#message").text("Someone downvoted! :( Upvote!");
                  } else {
                      $("#message").text("Upvote to see the result! (nothing new)");
                  }
                  originalScore = newScore;
              });
          }
          setTimeout(updateCounter, 1000);
      };

      updateCounter();
  });
});
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<div id="message">Upvote to see the result!</div>
<div id="counter">Time until next request: <span></span>
</div>
<div id="postCounter">This answer had <span></span> votes when you loaded the demo.</div>
added 1 character in body
Source Link
U13-Forward
  • 71.8k
  • 1
  • 17
  • 31
Updated to https
Source Link
CalvT
  • 3.2k
  • 1
  • 22
  • 28
$(document).ready(function() {
  $.getJSON("http"https://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
      var originalScore;
      console.log(data);
      $.each(data.items, function (i, v) {
          if (v.answer_id == 270116) {
              originalScore = v.score;
              return;
          }
      });

      $("#postCounter span").text(originalScore);

      var counter = 60;

      function updateCounter() {
          $("#counter span").text(counter);
          counter--;
          if (counter == 0) {
              counter = 60;
              $.getJSON("http"https://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
                  var newScore = 0;
                  $.each(data.items, function (i, v) {
                      if (v.answer_id == 270116) {
                          newScore = v.score;
                          return;
                      }
                  });
                  if (newScore > originalScore) {
                      $("#message").html("WOOHOO! You upvoted!<img src='httpsrc='https://i.imgur.com/B5aTato.gif' />");
                  } else if (newScore < originalScore) {
                      $("#message").text("Someone downvoted! :( Upvote!");
                  } else {
                      $("#message").text("Upvote to see the result! (nothing new)");
                  }
                  originalScore = newScore;
              });
          }
          setTimeout(function () {
              updateCounter();
          }, 1000);
      };

      updateCounter();
  });
});
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<div id="message">Upvote to see the result!</div>
<div id="counter">Time until next request: <span></span>
</div>
<div id="postCounter">This answer had <span></span> votes when you loaded the demo.</div>
$(document).ready(function() {
  $.getJSON("http://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
      var originalScore;
      console.log(data);
      $.each(data.items, function (i, v) {
          if (v.answer_id == 270116) {
              originalScore = v.score;
              return;
          }
      });

      $("#postCounter span").text(originalScore);

      var counter = 60;

      function updateCounter() {
          $("#counter span").text(counter);
          counter--;
          if (counter == 0) {
              counter = 60;
              $.getJSON("http://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
                  var newScore = 0;
                  $.each(data.items, function (i, v) {
                      if (v.answer_id == 270116) {
                          newScore = v.score;
                          return;
                      }
                  });
                  if (newScore > originalScore) {
                      $("#message").html("WOOHOO! You upvoted!<img src='http://i.imgur.com/B5aTato.gif' />");
                  } else if (newScore < originalScore) {
                      $("#message").text("Someone downvoted! :( Upvote!");
                  } else {
                      $("#message").text("Upvote to see the result! (nothing new)");
                  }
                  originalScore = newScore;
              });
          }
          setTimeout(function () {
              updateCounter();
          }, 1000);
      };

      updateCounter();
  });
});
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<div id="message">Upvote to see the result!</div>
<div id="counter">Time until next request: <span></span>
</div>
<div id="postCounter">This answer had <span></span> votes when you loaded the demo.</div>
$(document).ready(function() {
  $.getJSON("https://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
      var originalScore;
      console.log(data);
      $.each(data.items, function (i, v) {
          if (v.answer_id == 270116) {
              originalScore = v.score;
              return;
          }
      });

      $("#postCounter span").text(originalScore);

      var counter = 60;

      function updateCounter() {
          $("#counter span").text(counter);
          counter--;
          if (counter == 0) {
              counter = 60;
              $.getJSON("https://api.stackexchange.com/2.2/questions/269754/answers/?order=desc&site=meta.stackoverflow&client_id=3519&callback=?", function (data) {
                  var newScore = 0;
                  $.each(data.items, function (i, v) {
                      if (v.answer_id == 270116) {
                          newScore = v.score;
                          return;
                      }
                  });
                  if (newScore > originalScore) {
                      $("#message").html("WOOHOO! You upvoted!<img src='https://i.imgur.com/B5aTato.gif' />");
                  } else if (newScore < originalScore) {
                      $("#message").text("Someone downvoted! :( Upvote!");
                  } else {
                      $("#message").text("Upvote to see the result! (nothing new)");
                  }
                  originalScore = newScore;
              });
          }
          setTimeout(function () {
              updateCounter();
          }, 1000);
      };

      updateCounter();
  });
});
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<div id="message">Upvote to see the result!</div>
<div id="counter">Time until next request: <span></span>
</div>
<div id="postCounter">This answer had <span></span> votes when you loaded the demo.</div>
[Edit removed during grace period]
Source Link
nicael
  • 19.1k
  • 6
  • 57
  • 79
Loading
Post Made Community Wiki by animusonStaffMod
deleted 4 characters in body
Source Link
Ian
  • 6.2k
  • 1
  • 15
  • 10
Loading
added some cool stuff when you upvote
Source Link
Ian
  • 6.2k
  • 1
  • 15
  • 10
Loading
deleted 28 characters in body
Source Link
Ian
  • 6.2k
  • 1
  • 15
  • 10
Loading
added 2674 characters in body
Source Link
Ian
  • 6.2k
  • 1
  • 15
  • 10
Loading
added 2674 characters in body
Source Link
Ian
  • 6.2k
  • 1
  • 15
  • 10
Loading
Source Link
Ian
  • 6.2k
  • 1
  • 15
  • 10
Loading