Ajax/jQuery定时问题



我有一个点击函数,它在点击span时执行jQuery/Ajax$.post以从Web服务获取数据。当点击函数上设置了Firebug断点时,一切都会按预期进行(一些新的表tr会附加到表中)。如果没有断点集,则单击跨度时不会发生任何事情。Firebug没有显示任何错误。从其他stackoverflow问题中,我认为这是一个时间问题,但我不知道该怎么办。我曾尝试将$.post更改为$.ajax,并将async设置为false,但这并没有解决问题。以下是点击处理程序的代码:

$('.rating_config').click(function(event){
    event.preventDefault();
    event.stopPropagation();
    var that = $(this);
    // calculate the name of the module based on the classes of the parent <tr>
    var mytrclasses = $(this).parents('tr').attr('class');
    var modulestart = mytrclasses.indexOf('module-');
    var start = mytrclasses.indexOf('-', modulestart) + 1;
    var stop = mytrclasses.indexOf(' ', start);
    var mymodule = mytrclasses.substring(start, stop);
    mymodule = mymodule.replace(/ /g, '+');
    mymodule = mymodule.replace(/_/g, '+');
    mymodule = encodeURI(mymodule);
    // calculate the name of the property based on the classes of the parent <tr>
    var propertystart = mytrclasses.indexOf('property-');
    var propstart = mytrclasses.indexOf('-', propertystart) + 1;
    var propstop = mytrclasses.indexOf(' ', propstart);
    var myproperty = mytrclasses.substring(propstart, propstop);
    myproperty = myproperty.replace(/ /g, '+');
    myproperty = myproperty.replace(/_/g, '+');
    myproperty = encodeURI(myproperty);
    var parentspanid = $(this).attr('id');
    // Remove the comparison rows if they are already present, otherwise generate them
    if ($('.comparison_' + parentspanid).length != 0) {
        $('.comparison_' + parentspanid).remove();
    } else {
        $.post('http://localhost/LearnPHP/webservice.php?user=user-0&q=comparison&level=property&module=' + mymodule + '&version_id=1.0&property=' + myproperty + '&format=xml', function(data) {
            var data = $.xml2json(data);
            for (var propnum in data.configuration.modules.module.properties.property) {
                var prop = data.configuration.modules.module.properties.property[propnum];
                console.log(JSON.stringify(prop));
                prop.mod_or_config = 'config';
                var item_id = mymodule + '?' + prop.property_name + '?' + prop.version_id + '?' + prop.value;
                item_id = convertId(item_id);
                prop.id = item_id;
                //alert('prop.conformity = ' + prop.conformity);
                // genRow(row, module, comparison, comparison_parentspanid)
                var rowstring = genRow(prop, mymodule, true, parentspanid);
                console.log('back from genRow. rowstring = ' + rowstring);
                $(that).closest('tr').after(rowstring);
                //$('tr#node-' + data[row].id + ' span#rating' + row.id).css('background', '-moz-linear-gradient(left, #ff0000 0%, #ff0000 ' + data[row].conformity + '%, #00ff00 ' + 100 - data[row].conformity + '%, #00ff00 100%');
                var conformity_color = getConformityColor(prop.conformity);
                $('tr#comparison_module_' + mymodule + '_setting_' + prop.id + ' span#module_' + mymodule + '_rating' + prop.id).css({'background':'-moz-linear-gradient(left, ' + conformity_color + ' 0%, ' + conformity_color + ' ' + prop.conformity + '%, #fffff0 ' + prop.conformity + '%, #fffff0 100%)'});
                //$('tr#comparison-' + data[row].id + ' span#rating' + data[row].id).css('background','-webkit-linear-gradient(left,  #00ff00 0%, #00ff00 ' + data[row].conformity + '%, #ff0000 ' + (100 - (data[row].conformity + 2)) + '%, #ff0000 100%)');
            }
        });
        // Hide the Fix by mod column
        hideFixedByModCol();
        $('tr.comparison_' + parentspanid).each(function(i){
                if (i % 2 == 0) {
                    $(that).addClass('comparison_even');
                } else {
                    $(that).addClass('comparison_odd');
                }
            });
    }
  });

如有任何帮助,我们将不胜感激!

我怀疑您的数据返回时格式不正确。将中断时的代码封装在try {} catch {}中,以查看生成的错误。另外,在ajax请求中添加错误处理也是一个好主意。

最新更新