2013-02-18 68 views
-1

JAVASCRIPTAJAX刷新很慢

JEASearch = new Class({ 

Implements: [Options], 

     form: null, 

     forceUpdateLists : false, 

     options: { 
      fields : { 
       filter_search : "", 
       filter_area_id : 0, 
       filter_department_id : 0, 
       filter_town_id : 0 
      }, 
      useAJAX : true 
     }, 


     initFieldBehavior : function(fieldName) { 
      if (this.form[fieldName]) { 
       if (typeOf(this.form[fieldName]) == 'element') { 
        var field = document.id(this.form[fieldName]); 
        field.addEvent('change', function(event) { 
         this.forceUpdateLists = false; 
         this.options.fields[fieldName] = field.get('value'); 
         this.refresh(); 
        }.bind(this)); 

       } else if (typeOf(this.form[fieldName]) == 'collection') { 
        if (fieldName == 'filter_amenities[]') { 
         Array.from(this.form['filter_amenities[]']).each(function(item) { 
          item.addEvent('change', function(event) { 
           var index = this.options.fields.filter_amenities.indexOf(item.get('value')); 
           this.forceUpdateLists = true; 
           if (item.get('checked') && index == -1) { 
            this.options.fields.filter_amenities.push(item.get('value')); 
           } else if (!item.get('checked') && index > -1){ 
            this.options.fields.filter_amenities.splice(index, 1); 
           } 
           this.refresh(); 
          }.bind(this)); 
         }.bind(this)); 
        } else if (fieldName == 'filter_transaction_type') { 
         Array.from(this.form['filter_transaction_type']).each(function(item) { 
          item.addEvent('change', function(event) { 
           this.forceUpdateLists = true; 
           if (item.get('checked')) { 
            this.options.fields.filter_transaction_type = item.get('value'); 
           } 
           this.refresh(); 
          }.bind(this)); 
         }.bind(this)); 
        } 
       } 
      } 
     }, 

     refresh: function() { 
      if (this.options.useAJAX) { 
       var jSonRequest = new Request.JSON({ 
        url: 'index.php?option=com_jea&task=properties.search&format=json', 
        onSuccess: function(response) { 
         this.appendList('filter_area_id', response.areas); 
         this.appendList('filter_department_id', response.departments); 
         this.appendList('filter_town_id',response.towns); 

         this.form.getElements('.jea-counter-result').each(function(item){ 
          item.set('text', response.total); 
         }); 
        }.bind(this) 
       }); 

       jSonRequest.post(this.options.fields); 
      } 
     }, 

     reset : function() { 
      this.options.fields = { 
       filter_search : "", 
       filter_area_id : 0, 
       filter_department_id : 0, 
       filter_town_id : 0 

      }; 

      for (var fieldName in this.options.fields) { 
       if (this.form[fieldName]) { 
        if (typeOf(this.form[fieldName]) == 'element') { 
         var field = document.id(this.form[fieldName]); 
         if (field.get('tag') == 'select') { 
          field.set('selectedIndex', 0); 
         } 
         field.set('value', ''); 

        } else if (typeOf(this.form[fieldName]) == 'collection') { 
         Array.from(this.form[fieldName]).each(function(item) { 
          item.set('checked', ''); 
         }); 
        } 
       } 
      } 

      this.refresh(fast); 
     }, 

     appendList : function(selectName, objectList) { 
      if (this.form[selectName]) { 
       var selectElt = document.id(this.form[selectName]); 
       // Update the list only if its value equals 0 
       // Or if this.forceUpdateLists is set to true 
       if (selectElt.get('value') == 0 || this.forceUpdateLists) { 
        var value = selectElt.get('value'); 

        // Save the first option element 
        var first = selectElt.getFirst().clone(); 
        selectElt.empty(); 
        if (first.get('value') == 0) { 
         selectElt.adopt(first); 
        } 

        for (var i in objectList) { 
         if (objectList[i].text) { 
          var option = new Element('option', objectList[i]); 
          if (objectList[i].value == value) { 
           option.setProperty('selected', 'selected'); 
          } 
          selectElt.adopt(option); 
         } 
        } 
       } 
      } 
     } 

    }); 

上面的代碼工作我的網站的搜索欄上。當我選擇一個省/地區/城市 - 那些選擇內容開放非常緩慢,當我選擇另一個省而沒有點擊搜索按鈕時,需要一些時間來打開區和城市...
我粘貼上面的JAVASCRIPT代碼。你能解釋一下這是什麼原因嗎?

MY SITE

回答

0

綜觀Chrome開發工具的網絡選項卡,它記錄的請求毫不遲延地向服務器發,但服務器是不會這麼快作出反應。在我的連接上,每次響應時間大約爲500ms,最大時間超過2秒。

配置您的服務器端代碼。看來你正在使用PHP。

Simplest way to profile a PHP script