2011-06-25 12 views
0

jqueryを使用して、非同期リクエストではなくフルページリクエストを開始できますか?page request with jquery

私はテスト用アプリケーションのカウントダウンタイマーを実装しています。私は、ページの要求を通じて持続するタイミングが必要なので、私はブラウザではなくサーバ側の時間を追跡しています。私はjqueryスクリプトを毎秒アクションをポーリングして、時間をチェックし、残りの時間をhtml要素にレンダリングします。私のコード:

ビュー:

<div id='timer'></div> 

application.js:

setInterval(function(){ 
    $('#timer').load('/exams/time'); 
}, 1000); 

コントローラー:

def time 
    time = check_time 
    if time 
     render :text => time 
    else 
     stop_exam 
    end 
    end 

    def stop_exam 
    @exam = Exam.find(session[:exam]) 
    @exam.stop 
    reset_session 
    redirect_to @exam 
    end 

カウントダウンが正常に動作します。しかし、リダイレクト後、@examはページ全体を置き換えるのではなく、書き出します。おそらく、リダイレクトは、フルページリクエストではなく、AJAXリクエストとして扱っています。あなたはdiv要素にロードされたときにリダイレクトするというスクリプトを返すことができますどのように(それがポーリングを停止するように、JavaScriptなど?)私はページ全体を交換するstop_examを変更することができます

答えて

2

。私はルビーの構文を知らないが、次のようなものを返す:

<script type='text/javascript'> 
    document.location='http://www.google.com'; 
</script> 
+0

ありがとうございました!これを読んだ他の人のために、レール構文は次のようにレンダリングされます:js => "" –

0

このコードをyorビューに加える。 idが 'count'のdivを1つ追加します。

<script type="text/javascript" > 
window.onload = function() 
{ 
    startCountdown(<%[email protected]_time%>) 
} 
</script> 
<script type="text/javascript"> 
function startCountdown(g_iCount) 
{ 
    if((g_iCount) > 0) 
    { 
     var str="green"; 
     if (g_iCount<20&&g_iCount>=10) 
     { 
      str="orange" 
     } 
     else if (g_iCount<10) 
     { 
      str="red" 
     } 


     document.getElementById('count').innerHTML=g_iCount.toString().fontcolor(str); 

     g_iCount = g_iCount - 1; 

     setTimeout(function() { 

      startCountdown(g_iCount); 

     }, 1000); 
    } 
    else 
    { 
     document.getElementById('procedure_submit_button').click(); 
    } 
} 
</script> 

ザッツall.Yourは、ページのロード時に始まりますカウントダウン。