Advertisement
Guest User

Untitled

a guest
Apr 17th, 2015
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var timers = []; // Creates an array to store your timers
  2. var socket = io.connect('http://localhost:8000');  
  3. socket.on('notification', function (data) {
  4.     $.each(data.auctions,function(index,auction){  
  5.     duration = auction.duration;
  6.    
  7.     if (duration > 0)
  8.     {
  9.     var price_retail = $("#" + auction.id).attr("data-retail");
  10.     var discount = ((100) - (Math.floor(auction.price/price_retail*100)));
  11.     var duration_html = formatSeconds(duration);
  12.     var calc = auction.price.toFixed(2);
  13.     var price_calc = calc.toString().replace(/\./g, ',');  
  14.     $(".price" + auction.id).html(price_calc + "€");         
  15.     $(".discount" + auction.id).html(discount + "%");      
  16.  
  17.     for(i in timers) {
  18.         if ($('.price' + auction.id).data('curr_text') !== $('.price' + auction.id).text())
  19.         {
  20.         $(".flashing" + auction.id).toggle("highlight").toggle("highlight");           
  21.         timers[i][1] = 10; // If timer for data.user already exists, set it to 10 seconds again.
  22.         } else {
  23.         timers.push([auction.user, duration]); // Else, create it with data.seconds seconds
  24.         }
  25.     }  
  26.    
  27.     $('.price' + auction.id).data('curr_text', $('.price' + auction.id).text());       
  28.     } else {
  29.     // auction ended   
  30.     }
  31.     });
  32. });
  33.  
  34. function timerCount() {
  35.     for(i in timers) {
  36.         console.log(timers[i]);
  37.        
  38.         if(timers[i][1] <= 0) {
  39.             delete timers[i]; // If timer seconds is less than 0, delete it.
  40.         } else {
  41.             timers[i][1]--; // Else, decrease it by 1 second.
  42.         }
  43.     }
  44. }
  45. setTimeout(timerCount, 1000); // Runs the timerCount() function every second.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement