-1
function startCampaign(index, campaign_id) {
    var btn = $('#toggleStateBtn' + index);
    btn.html('<i class="icofont-pause"></i>');
    btn.removeClass('btn-success');
    btn.addClass('btn-warning');
    btn.attr('title', 'Pause this campaign');
    btn.width('9px');

    // Start campaign
    $.ajax({
        url:    '/campaign/startCampaign/' + campaign_id,
        type:   'POST',
        success: function(data){
            // Reflect the new state in status column
            btn.html(data);
        }
    });
}

function pauseCampaign(index, campaign_id) {
    var btn = $('#toggleStateBtn' + index);
    btn.html('<i class="icofont-play"></i>');
    btn.removeClass('btn-warning');
    btn.addClass('btn-success');
    btn.attr('title', 'Run this campaign');
    btn.width('9px');

    // Pause campaign
    $.ajax({
        url:    '/campaign/pauseCampaign/' + campaign_id,
        type:   'POST',
        success: function(data){
            // Reflect the new state in status column
            btn.html(data);
        }
    });
}

For some reason this is causing strange behaviour. If you replace btn with $('#toggleStateBtn' + index) in all places then everything works fine.

My guess is, if for instance you first call startCampaign() and then called pauseCampaign() the old btn reference gets changed to the new one. But I'm not sure.

Ideas?

4
  • 1
    What's the error you receive? Commented Jun 11, 2013 at 14:47
  • Not an error, just strange behaviour. Like I'm seeing the ajax response in the button instead of the button icon. Commented Jun 11, 2013 at 14:50
  • I'm a little confused, could you explain what this code is doing vs. what you expect it to do? Commented Jun 11, 2013 at 14:53
  • I believe it's because of caching (maybe not the right word to describe what I'm talking about). It saves the state of that variable at that time, instead of updating. Commented Jun 11, 2013 at 14:53

1 Answer 1

1

Nevermind I am an idiot, the btn in btn.html(data) is actually a different control. I was mindlessly substituting. Looks like I need an extra dose of caffeine this morning.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.