Ver Mensaje Individual
  #1 (permalink)  
Antiguo 23/11/2010, 06:35
sreon
 
Fecha de Ingreso: septiembre-2010
Mensajes: 43
Antigüedad: 13 años, 7 meses
Puntos: 0
Hash: No entiendo el script

Hola estoy intentando comprender un código de una pagina,sobre pasarle el hash con # tal y como hace tuenti,y que luego haga una petición AJAX,entiendo casi todo el código pero no entiendo una cosa,que es lo que hace la funcion verify_hash() y la variable page_timer,dejo aqui el script y la pagina donde lo saco:
A ver si podeis ayudarme,gracias
Cita:
/* AJAX request example with hashes */
/* by Adrián Navarro (http://adrian.navarro.at) */
/* <[email protected]> */

/* Monitors the URL for a hash, and loads the */
/* content through a standard AJAX request using */
/* the jQuery framework. */

/* Useful for heavy '2.0' applications using lots */
/* of AJAX. This also enables the end-user to use */
/* the "back" button from his browser. */

var default_hash = null;
var error_timer = null;
var page_timer = null;

var error_timeout = 12000;
var timer_wait = 500;

function launch() {
/* should do:
1. set the page load timer
2. set the error throw timer (went wrong, stop everything, retry)

also check if there's an hash, send it

first time we load:
- get the hash (there's?), send it through ajax. use std functions
*/

load_page(window.location.hash);
}

function load_page(hash) {
/*
most important function: load the webpage (through ajax)
- sets an error timer that will be removed if it's loaded before 'error_timeout' in msec
- sets the default hash to the hash we are going to work with (for verifyhash() interval)
- create an AJAX request url (formality...)
- then throw AJAX request and redirect it to load_page_success function
*/

error_timer = setTimeout("declare_error()", error_timeout);

default_hash = hash;
hash = clean_hash(hash);
if(hash.length > 0) hash = "?" + hash;

$.ajax({type: "GET", url: "pageloader.php" + hash, success: load_page_success});
}

function clean_hash(hash) {
/*
this function removes the # if given in the 'hash' string
some browsers include this in the window.location.hash, dunno
*/

new_hash = hash.replace(/^\#/, "");
return new_hash;
}

function declare_error() {
/*
if "shit happens": (yeah, it often does..)
- clear all timers
- hide all shown (or not) divs
- show the awesome 'error' div
*/

if(error_timer) clearTimeout(error_timer);
if(page_timer) clearTimeout(page_timer);

document.getElementById('nojs').style.display='non e';
document.getElementById('jscontent').style.display ='none';
document.getElementById('error').style.display='';
}

function load_page_success(html) {
/*
we've got: html
also, we have to remove the error timer and re-activate the hash checking timer
*/

clearTimeout(error_timer);
$("#jscontent").html(html);
page_timer = setTimeout("verify_hash()", timer_wait);
}

function verify_hash() {
/*
checks if we haven't loaded the webpage already
then fires loadpage() to load through ajax

also re-initiates (in a loop) the verify_hash timer
*/

page_timer = setTimeout("verify_hash()", timer_wait);

if(default_hash == window.location.hash) return;
load_page(window.location.hash);
}
Fuente: http://adrian.navarro.at/demo/hash/scripts.js