简体   繁体   中英

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. 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). When there is no break point set, nothing happens when you click the span. Firebug doesn't show any errors. I assume from other stackoverflow questions that this is a timing problem, but I don't know what to do about it. I have tried changing from a $.post to a $.ajax and setting async to false, but that didn't fix it. 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. Also it would be a good idea to add error processing to your ajax request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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