wait = -> $.ajax url: window.location.pathname + '/wait' dataType: 'json' timeout: 29 * 1000 success: -> $.get window.location.pathname + '.js', (html)-> $('#book').html html initMathJax() initNavOffset() wait() error: (jqXHR, textStatus)-> if textStatus == 'timeout' # polling end, reconnect immediately wait() else # server down, try reconnecting again after timeout setTimeout wait, 5000 initMathJax = -> $('#mathJaxJS').remove() delete MathJax chapter_number = $('.chapter').attr('data-number') script = document.createElement( 'script' ) script.id = 'mathJaxJS' script.type = 'text/javascript' script.src = '<%= @mathjax_src %>' script.innerHTML = """ <%= @mathjax_config %> """ $('head').append script delay = 1000 initNavOffset = -> loading = true finish = _.debounce -> loading = false , delay offset = 20 navHeight = $('#bookMenu').height() + offset $(window).scroll -> if loading offset = $(window.location.hash).offset().top - navHeight $(window).scrollTop offset finish() $(window).bind 'hashchange', (e)-> e.preventDefault() offset = $(window.location.hash).offset().top - navHeight $(window).scrollTop offset $ -> initMathJax() initNavOffset() wait()