JavaScript an algorithm for time sequencing (multiple countdown sort for a page)

Source: Internet
Author: User


last week to do an activity page seconds to kill the list page needs a time to sort out the algorithm to think about the various algorithms have not come out, and later asked the next background of the PHP classmate he wrote an algorithm to me to see the next, just start to think that this is a pure algorithm, can not be converted into a page Dom effect, But looked again two times found can so I changed the implementation of the to be able to sigh is indeed pretty praise of the Bo a guest;

The page requirements are: from 11 to 20 every one hours a second kill if it is the current time to show the product before the second kill, and so on the last row

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

Page Demo please scan the code

This is the back-end classmate of the pure algorithm

functionShow_test (hour) {p= [' Dian ', ' dian ', ' dian ', ' dian ', ' dian ', ' dian ', ' dian ', ' Dian ']; Console.log (' The original order is ');            Console.log (P); Date=NewDate (); Curhour=date.gethours (); POS= curHour-13; //pos = hour;s = ' active ' + p[pos]+ "in progress";            Console.log (s); Desc= ' The 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"); } callsvarCurhour=NewDate (). getHours (); Show_test (curhour);

The perfect implementation of this algorithm needs the kind of requirements expressed in the form of the problem is how to really convert to the page, so I will be the perfect realization of it;

//First, you define an array of IDs and pictures that contain the items that are killed per second img1 is a product picture Img2 is a second kill time IMG3 is a product descriptionvarData=[{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:"666.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, IMG1:"10.jpg", Img2:"101.jpg", IMG3:"1010.jpg"  }  ];//Object Array SortingfunctionCompare (PropertyName) {return function(Object1, object2) {varValue1 =Object1[propertyname]; varvalue2 =Object2[propertyname]; if(Value2 <value1) {       return-1; }Else if(Value2 >value1) {        return1; }Else {        return0; }   } } //because now the array has become a complex array, the sort is used to sort the song method according to the object's attributes .//This method is mentioned in JavaScript advanced programming. functionItemshow (hour) {p=data; //Current TimeCurhour =hour; //array subscript for the corresponding timePOS= curHour-11; if(pos<=0) {//if it doesn't go to 11 o'clock, it shows the first order.Pos=0; }Else if(pos>=9) {//if it's over 20, it's completely reversed.Pos=9} s= ' Active ' + p[pos]+ ' in progress ';            Console.log (s); //reverse-Sort the property based on the time in the arrayP.reverse (Compare ("Time"));            Console.log (POS);            Console.log (P); //define a temporary array to store obsolete itemsTMP = []             for(i = 0; i<pos; i++) {Tmp.push (P.pop ()); }            //Reorder the remaining re-phasesP.reverse (Compare ("Time")); //Link a product item that has not been killed to a second time to an array that has expiredp = p.concat (Tmp.reverse (Compare ("time"));//Console.log (DESC);            Console.log (P);  for(vari=0;i<data.length;i++){              if(i==0) {//is killing in seconds .$ (". Item"). EQ (0). Append (" ")                $(". Item"). EQ (0). Append (")                $(". Item"). EQ (0). Append (")              }Else{                $(". Item"). EQ (i). Append (" ")                $(". Item"). EQ (i). Append (")                $(". Item"). EQ (i). Append (")              }            }                    }

So the algorithm is implemented into the page to show that the algorithm is still to strengthen the possibility of my transformation is not perfect, but still can feel the beauty of the algorithm
This is the PHP little buddy of the algorithm: Blog.csdn.net/typeof_

JavaScript an algorithm for time sequencing (multiple countdown sort for a page)

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.