JavaScript time Sorting algorithm realizes activity second kill countdown effect _javascript skill

Source: Internet
Author: User
Tags array sort

Make an active page seconds kill list page need a time algorithm sort myself pondering for a while to think of a variety of algorithms did not get out, later asked the next backstage PHP classmate he wrote an algorithm for me to see the next, just start to see that this is a pure algorithm, can not be converted into a page of the DOM effect, But saw two times to find can, so I changed the change, realized, first share to everyone.

Page requirements are: from 11 to 20 point every one hours a second kill if it is the current time to show the goods before the second kill to the last row and so on

Similar to the first 11-point order is 11,12,13,14,15,16,17,18,19,20 (dots);
The order to 12 points is 12,13,14,15,16,17,18,19,20,11 (dots)
The order to 13 points is 13,14,15,16,17,18,19,20,12,11 (dots)
....
The final order is 20,19,18,17,16,15,13,12,11 (dots)

This is the pure algorithm of the backstage classmate

function show_test (hour)
  {
   p = [' Dian ', ' dian ', ' dian ', ' dian ', ' dian ', ' dian ', ' dian ', ' Dian '];
   Console.log (' original order is ');
   Console.log (p);
   Date = new Date ();
   Curhour = Date.gethours ();
   pos = curHour-13;
   pos = hour;
   s = ' activity ' + p[pos]+ ' is in progress ';
   Console.log (s);
   desc = ' Current order should be ';
   P.reverse ();
   Console.log (POS);
  
   TMP = [] for
   (i = 0; i<pos; i++) {
    Tmp.push (P.pop ());
   }
   P.reverse ();
   p = P.concat (Tmp.reverse ());
   Console.log (DESC);
   Console.log (p);
   Console.log ("n \ nthe");
  }

Call

var curhour=new Date (). getHours ();
Show_test (Curhour);

This algorithm is perfect to achieve the need for the kind of requirements expressed in the appearance of the problem is how to really convert to the page, so I think after I will be perfect to achieve;

First define an array that contains the IDs and pictures of the items that are killed per second img1 is the product picture Img2 is the second kill time IMG3 is the product description var data=[{id:1, time:11, img1: "1.jpg", Img2: "11.jpg", IMG3: "111.jpg"}, {id:2, time:12, img1: "2.jpg", Img2: "22.jpg", Img3: "222.jpg"}, {id:3, time:13, IMG1: "3 . jpg ", Img2:" 33.jpg ", Img3:" 333.jpg "}, {id:4, time:14, img1:" 4.jpg ", Img2:" 44.jpg ", Img3:" 444.jpg "}, {ID: 5, Time:15, IMG1: "5.jpg", Img2: "55.jpg", Img3: "555.jpg"}, {id:6, time:16, img1: "6.jpg", Img2: "66.jpg", Img3: "6 66.jpg "}, {id:7, time:17, img1:" 7.jpg ", Img2:" 77.jpg ", Img3:" 777.jpg "}, {id:8, time:18, img1:" 8.jpg ", Img2: "88.jpg", Img3: "888.jpg"}, {id:9, time:19, img1: "9.jpg", Img2: "99.jpg", Img3: "999.jpg"}, {id:10, time:20, I

MG1: "10.jpg", Img2: "101.jpg", Img3: "1010.jpg"}]; 
 Object array sort function Compare (PropertyName) {return function (Object1, object2) {var value1 = Object1[propertyname]; 
 var value2 = Object2[propertyname]; 
 if (value2 < value1) {return-1; }else if(Value2 > value1) 
 {return 1; 
 }else {return 0; }}//Because now the array has become a complex array so sort to use the song method based on one of the object's properties//This method has mentioned function itemshow (hour) {P=d in the JavaScript Advanced program design.
   Ata 
   Current Time Curhour = hour;
   The corresponding time of the array subscript pos = curHour-11;
   if (pos<=0) {//If the first order pos=0 is displayed before 11 o'clock;
   }else if (pos>=9) {//If more than 20 points in full reverse order pos=9} s = ' activity ' + p[pos]+ "in progress";
   Console.log (s);
   Reverse-Sort the p.reverse (compare ("time") according to the Times in the array);
   Console.log (POS);
   Console.log (P);
   Defines a temporary array to store obsolete commodity item TMP = [] for (i = 0; i<pos; i++) {Tmp.push (P.pop ());
   ///P.reverse The remainder of the reverse phase (compare ("time")); The item with the expired array link p = p.concat (Tmp.reverse (Compare ("time")) that has not been killed for the second time;
   Console.log (DESC);

   Console.log (P); for (Var i=0;i<data.length;i++) {if (i==0) {//is in seconds to kill $ (". Item"). EQ (0). Append (" '}else{$ (". Item"). EQ (i) Append (" 

This will implement the algorithm into a page display, I hope you can have some harvest, understand the JavaScript time sorting algorithm.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.