php - Improve performance in jquery when change in drop down value -
i have drop down list , when changing value hiding , displaying other fields.currently executing 1s in 8gb , other pc 2g or 4g takes 5s execute code. in firefox takes more time compared chrome , safari.
my code:
<?php $this->widget('application.extensions.select2.xselect2', array( 'model'=>campaignpostcodemodel::model(), 'attribute'=>'crm_campaign_post_code_id', 'data'=> $tt_fields, 'events'=>array( 'change'=>"js:function (element) { recalldelay(this);}"), 'htmloptions'=>array( 'style'=>'width:220px', 'id'=>'postcode', 'prompt'=>utf8_encode(yii::t('operator','selectfield')) ),)); ?> <script type="text/javascript"> function recalldelay(elem){ $('#ok').attr('disabled',true); $.ajax({ type: 'post', url: "<?php echo ccontroller::createurl('operator/recalldelay'); ?>", update: '#postcode_id', data: {"postcode_id":document.getelementbyid("postcode").value,"taskid":'<?php echo $taskid?>',"team":'<?php echo $is_team?>'}, success: function(data){ $('#recallby').select2('destroy'); $('#recallby').prop('disabled', true); $("#recallby").html(''); $('#recallby').select2(); $('#recalldate').attr('disabled','disabled'); $("#trans_recallby").html(''); $("#trans_recalldate").val(''); $('#recalldate').val(''); $('#lbl_trans_recalltime').hide(); $('#trans_recalldate').hide(); $('#lbl_trans_recallby').hide(); $('#trans_recallby').select2('container').hide(); $('#fld_set').hide(); $('#fld_trans_set').hide(); if (data.trim() != "" && data != null) { var obj = data.split(","); if(obj[0] == "standard"){ $('#fld_set').show(); $("#recallby").html(""); $('#recallby').select2('destroy'); if (<?php echo $is_team?> != 1) $('#recallby').prop('disabled', true); else $('#recallby').prop('disabled', false); $('#recallby').select2(); var $str = $.trim(obj[1]).substr (0,$.trim(obj[1]).lastindexof (" ") + 1); var $date = $str.split("-"); var $str1 = $.trim(obj[1]).substr ($.trim(obj[1]).lastindexof (" ") + 1,$.trim(obj[1]).length); var $time = $str1.split(":"); $("#recalldate").datetimepicker({ dateformat: 'yy-mm-dd h:i' }).datetimepicker("setdate",new date($date[0],$date[1]-1,$date[2],$time[0],$time[1] )); } else if(obj[1] == "assigned"){ $('#fld_set').show(); $("#recallby").html(obj[0]); $('#recallby').select2('destroy'); $('#recallby').prop('disabled', false); $('#recallby').select2(); $('#cal_btn').show(); var str = $.trim(obj[2]).substr (0,$.trim(obj[2]).lastindexof (" ") + 1); var date = str.split("-"); var str1 = $.trim(obj[2]).substr ($.trim(obj[2]).lastindexof (" ") + 1,$.trim(obj[2]).length); var time = str1.split(":"); $("#recalldate").datetimepicker({ dateformat: 'yy-mm-dd h:i' }).datetimepicker("setdate",new date(date[0],date[1]-1,date[2],time[0],time[1] )); } else if($.trim(obj[0]) == "previous"){ $('#fld_set').show(); $('#cal_btn').hide(); if (<?php echo $is_team?> != 1) $('#recallby').prop('disabled', true); else $('#recallby').prop('disabled', false); $('#recalldate').prop('disabled','disabled'); } else if(obj[1] == "transfer"){ if (obj[2] == 1) { $('#fld_trans_set').show(); $('#lbl_trans_recalltime').show(); $('#trans_recalldate').show(); } if (obj[3] == 1) { $('#fld_trans_set').show(); $('#lbl_trans_recallby').show(); $("#trans_recallby").html(obj[0]); $('#trans_recallby').select2('destroy'); $('#trans_recallby').prop('disabled', false); $('#trans_recallby').select2(); $('#trans_cal_btn').show(); } if (obj[3] == 0 || obj[3] == 2) { $('#fld_trans_set').show(); $('#lbl_trans_recallby').show(); $("#trans_recallby").html(obj[0]); $('#trans_recallby').select2('destroy'); $('#trans_recallby').prop('disabled', true); $('#trans_recallby').select2(); if (obj[3] == 2) $('#trans_cal_btn').show(); else $('#trans_cal_btn').hide(); } } else if($.trim(obj[0]) == "successrecall") { $('#fld_trans_set').hide(); $('#fld_recallby').hide(); $('#fld_set').show(); $('#recalldate').removeattr('disabled'); $("#recalldate").datetimepicker({ dateformat: 'yy-mm-dd h:i', timeformat: 'hh:mm' }).datetimepicker("setdate",new date()); } else{ $('#fld_set').show(); $("#recallby").html(obj[0]); $('#recallby').select2('destroy'); if (<?php echo $is_team?> != 1) $('#recallby').prop('disabled', true); else $('#recallby').prop('disabled', false); $('#recallby').select2(); $('#cal_btn').show(); var str = $.trim(obj[1]).substr (0,$.trim(obj[1]).lastindexof (" ") + 1); var date = str.split("-"); var str1 = $.trim(obj[1]).substr ($.trim(obj[1]).lastindexof (" ") + 1,$.trim(obj[1]).length); var time = str1.split(":"); $("#recalldate").datetimepicker({ dateformat: 'yy-mm-dd h:i' }).datetimepicker("setdate",new date(date[0],date[1]-1,date[2],time[0],time[1] )); } if($.trim(obj[0]) != "previous") $('#recalldate').removeattr('disabled'); } else{ $('#fld_set').hide(); $('#fld_trans_set').hide(); } $('#ok').attr('disabled',false); } }); } </script>
please me improve performance of code execute in 1s in pcs.
try find out on code fragment time spend in big volume. can insert time stamp before , after code block find out how long takes or use 3rd party program (such yslow).
then need analyze fragment see if optimized.
from noticed looking @ code:
eliminate duplicate jquery searches reusing variable i.e.
var $recallby = $('#recallby'); $recallby.html(obj[0]); $recallby.select2('destroy'); // etc
instead of using if
(val=1) else if (val=2)
.. useswitch(val)
i.e.switch(obj[1]) { case 'standard' : <code> case 'previous' : <code> case 'successrecall' : <code> // etc }
Comments
Post a Comment