Guest User

Untitled

a guest
Jul 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. function createCounter(elementId,start,end,totalTime,callback)
  2. {
  3. var jTarget=jQuery("#"+elementId);
  4. var interval=totalTime/(end-start);
  5. var intervalId;
  6. var current=start;
  7. var f=function(){
  8. jTarget.text(current);
  9. if(current==end)
  10. {
  11. clearInterval(intervalId);
  12. if(callback)
  13. {
  14. callback();
  15. }
  16. }
  17. ++current;
  18. }
  19. intervalId=setInterval(f,interval);
  20. f();
  21. }
  22. jQuery(document).ready(function(){
  23. createCounter("counter",12714086+'',9999999999,10000000000000,function(){
  24. alert("finished")
  25. })
  26. })
  27.  
  28. var num = 3874923.12 + ''; //converts to a string
  29. numArray = num.split('.'); //numArray[0] = 3874923 | numArray[1] = 12;
  30.  
  31. commaNumber = '';
  32. i = numArray[0].length;
  33. do
  34. {
  35. //we don't want to start slicing from a negative number. The following line sets sliceStart to 0 if i < 0. Otherwise, sliceStart = i
  36. sliceStart = (i-3 >= 0) ? i-3 : 0;
  37.  
  38. //we're slicing from the right side of numArray[0] because i = the length of the numArray[0] string.
  39. var setOf3 = numArray[0].slice(sliceStart, i);
  40.  
  41. commaNumber = setOf3 + ',' + commaNumber; //prepend the new setOf3 in front, along with that comma you want
  42.  
  43. i -= 3; //decrement i by 3 so that the next iteration of the loop slices the next set of 3 numbers
  44. }
  45. while(i >= 0)
  46.  
  47. //result at this point: 3,874,923,
  48.  
  49. //remove the trailing comma
  50. commaNumber = commaNumber.substring(0,commaNumber.length-1);
  51.  
  52. //add the decimal to the end
  53. commaNumber += '.' + numArray[1];
  54.  
  55. //voila!
Add Comment
Please, Sign In to add comment