![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
Jorge a écrit : 20.49/100 ms (SAM) Ha Yes ... not so good :-( |
|
9.97/100 ms (Thomas) this one when I tried it re-display the original ordered table with a step of 4 9.09/100 ms (Jorge) Bon sang ! Mais bien sûr ! the famous appendChild that doesn't only add an element but can also move it Does this also return to the initial position every 4 times ? |
#12
| |||
| |||
|
|
Thomas 'PointedEars' Lahn wrote: sanju wrote: I have html table and this table contains 10 Rows and 2 column, I want every time this HTML page is called by the user to view the rows Randomly. How can I do this from JavaScript? Quick hack: /** * @return A pseudo-random IEEE-754 double in the interval * <tt>[0, 1)</tt>. * @type number */ function prng() { var r = Math.random(); // Opera bug workaround if (r == 1) r = 0; return r; } /** * Returns a pseudo-random integer value in the interval * <tt>[0, top)</tt>, or in the interval <tt>[bottom, top)</tt * if <code>bottom</code> was provided. * * @param top : number * @param bottom : optional number * @return pseudo-random integer value in the specified interval * @type number */ function prng_int(top, bottom) { if (!bottom) bottom = 0; return Math.floor(prng() * (top - bottom)) + bottom; } /** * Sorts the rows of the first table body of the document randomly. */ function bodyLoad() { // get table object reference var t = ... if (t) { // get table body object reference var tbody = (t.tBodies || [])[0]; if (tbody) { for (var rows = tbody.rows, len = rows.length, i = len; i-- ![]() { tbody.insertBefore(rows[i], rows[prng_int(len)]); } } } } body onload="bodyLoad()" Hmmm, this code shuffles the rows in just 4 lines : var tbody= (document.getElementById('myTable')).tBodies[0], rows= tbody.rows, i; for (i= rows.length; i; i--) { tbody.appendChild(rows[Math.floor((i)*Math.random())]); } And runs ~10% faster : http://tinyurl.com/56g37t 20.49/100 ms (SAM) 9.97/100 ms (Thomas) 9.09/100 ms (Jorge) |
![]() |
| Thread Tools | |
| Display Modes | |
| |