0

別のサーバーでホストされている json ファイルを検索する javascript/ajax 関数があります。次のアクションを実行するには、関数が必要です。

  1. 外部サーバーからjsonファイルを取得する
  2. 独自のローカル サーバーに json ファイルを保存する
  3. json ファイルが 1 時間以上経過しているかどうかを確認する
  4. 1 時間以上経過していない場合は、そのデータを使用します
  5. 1 時間以上経過している場合 - 外部サーバーから再ダウンロードし、ローカル バージョンを上書きします

現在、私の関数は、呼び出されるたびに外部サーバーからデータを取得します。このキャッシング機能を追加する必要がありますが、その方法がわかりません。

誰でもアドバイスを提供できますか?

ここに私のコード:

       function ajaxLoad(page,url,type,variety,category , widgetClickedId)
       {         

            /*
             * Checking the clicked id is the same as passed in one
             * TODO refactor so the clicked id only ever gets passed 
             */
            if(widgetId != widgetClickedId && widgetClickedId != undefined)
            {
                return false;
            }

            var website = $('#button-print'+widgetId).data("website");          
            var page = $('#button-print'+widgetId).data("page");


            $.ajax({
                type: 'GET',                   
                url:  'www.myothersite.com/api/v1/productchoice.json?website='+website,
                async: true,
                jsonp: 'callback',
                dataType: 'jsonp',
                success: function(productchoice)
                { 

                    if(productchoice['brand'+widgetId] == 'null' || productchoice['brand'+widgetId] == undefined)
                    {
                        productchoice['brand'+widgetId] = '';
                    }
                    //check that all values are not null , if not , then show the widget                    
                    if(        productchoice['brand'+widgetId] == ''                                    
                            && productchoice['subject'+widgetId] == '' 
                            && productchoice['market'+widgetId] == ''
                            && productchoice['type'+widgetId] == ''
                            && productchoice['bookazinebrands'+widgetId] == '')
                    {                       

                        //is this corect?                           
                        $('#gdmContainer'+widgetId).hide();
                        //return false;
                    }
                    else
                    {                   
                        $('#gdmContainer'+widgetId).show();                         
                    }

                    setRibbons(productchoice.ribbonTop , productchoice.ribbonBottom);
                    getProductChoice( productchoice );    
                    setLoveTitle( productchoice);
                    setDefaultBtn(productchoice['defaultBtn'+widgetId]);                     
                    return productchoice; 
                }               
            });    

前もって感謝します!

4

1 に答える 1

0

必要なのは、Ajax 要求から返されたデータを使用して JavaScript オブジェクトを作成することだけです。そのオブジェクトの一部として、オブジェクトがキャッシュされた日付を保存します。その後、関数を呼び出すたびに、その日付と現在の時刻を比較できます。

これが私がこれに取り組む方法です:

     // Variable to contain cached object. Make sure it name it better...
     var x = null;

     function ajaxLoad(page, url, type, variety,category, widgetClickedId)
     {
        /*
        * Checking the clicked id is the same as passed in one
        * TODO refactor so the clicked id only ever gets passed 
        */
        if(widgetId != widgetClickedId && widgetClickedId != undefined)
        {
            return false;
        }

        var website = $('#button-print'+widgetId).data("website");          
        var page = $('#button-print'+widgetId).data("page");

        // Creating a new date and adding an hour to it.
        var d = new Date();
        d.setHours(d.getHours() + 1);

        // Test to ensure x is not null and it's not older than an hour old
        if (x != null && Date.parse(x.date) < d) {
            // Do your stuff with the cached file here
        }
        else {
            $.ajax({
                // set up your ajax request
                ...
                success: function (productchoice) {
                    // Make sure to cache the object
                    x.file = productchoice;
                    x.date = new Date();

                    // Do your stuff with the cached file here
                }
            });
        }
     }

コードの繰り返しを避けるために、通常の「成功」操作を別の関数に入れると便利です。これを行う場合、変数 (つまり)xを使用する代わりに、キャッシュされた変数 (サンプル コード内) を使用します。productchoicex.file['brand' + widgetId]

于 2013-06-05T17:03:54.497 に答える