简体   繁体   English

Ajax / jQuery计时问题

[英]Ajax/jQuery Timing Issue

I have a click function that does a jQuery/Ajax $.post to get data from a webservice when a span is clicked. 我有一个单击功能,当单击跨度时,它执行jQuery / Ajax $ .post以从Web服务获取数据。 When there is a Firebug break point set on the click function, everything works as expected (some new table tr's are appended to a table). 如果在click函数上设置了Firebug断点,则一切都会按预期进行(某些新表tr会附加到表中)。 When there is no break point set, nothing happens when you click the span. 如果未设置断点,则单击跨度时不会发生任何事情。 Firebug doesn't show any errors. Firebug没有显示任何错误。 I assume from other stackoverflow questions that this is a timing problem, but I don't know what to do about it. 我从其他stackoverflow问题中假设这是一个计时问题,但是我不知道该怎么办。 I have tried changing from a $.post to a $.ajax and setting async to false, but that didn't fix it. 我尝试将$ .post更改为$ .ajax并将async设置为false,但这并不能解决问题。 Here's the code for the click handler: 这是点击处理程序的代码:

$('.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');
                }
            });
    }

  });

Any help would be greatly appreciated! 任何帮助将不胜感激!

I suspect your data is coming back improperly formed. 我怀疑您的数据返回格式不正确。 Enclose your code from the break on within try {} catch {} to see the error generated. 将您的代码从try {} catch {}结尾处封闭起来,以查看生成的错误。 Also it would be a good idea to add error processing to your ajax request. 将错误处理添加到您的ajax请求中也是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM