0

I'm trying to automatically run the onclick function in one button placed in phtml template.

This is the html file with the button code:

 <button type="button" id="review-btn" title="<?php echo $this->__('Place Order') ?>" class="button btn-checkout" onclick="review.save();"><span><span><?php echo $this->__('Place Orderxxxxx') ?></span></span></button>

This is part of javascript file with save and review functions:

//review function starts 
var Review = Class.create();
Review.prototype = {
    initialize: function(form,saveUrl,successUrl,agreementsForm){
        this.form = form;
        this.saveUrl = saveUrl;
        this.successUrl = successUrl;

        this.agreementsForm = agreementsForm;
        this.onSave = this.nextStep.bindAsEventListener(this);
        this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
    },
      //function triggers when onloading on review save function
    loadingbox: function () {
         var translate = Translator.translate('processing').stripTags();
        $("review-please").update(' <div class="please-wait-loading">&nbsp;</div><span class="load-wait">'+translate+'</span>')
    var form = $('review-btn');
    form.disabled='true';

    },

    save: function(){
 var paymentmethod = payment.currentMethod;
        var validator = new Validation(this.form);
        if (validator.validate()) {


            var request = new Ajax.Request(
                this.saveUrl,
                {
                    method:'post',
                    parameters: Form.serialize(this.form),
                    onLoading:this.loadingbox.bind(this),
                    onComplete: this.onComplete,
                    onSuccess: function(transport)    {
                        if(transport.status == 200)    {
                            var data = transport.responseText.evalJSON();
                            if(!data.success)
                            {
                                alert(data.error_messages);
                                $("review-please").update('');
                                 $('review-btn').disabled='';

                            }
                            if (data.redirect) {
                                location.href = data.redirect;
                                return;
                            }
                            if(data.success){
                               //hostedpro and advanced payment action
                                if(paymentmethod == 'hosted_pro' || paymentmethod =='payflow_advanced')
                                {
                                        Element.hide('review-please');
                                        Element.hide('review-btn');
                                        document.getElementById('checkout-paypaliframe-load').style.display= 'block';
                                        iframedata = data.update_section["html"].replace("display:none","display:block");
                                        document.getElementById('checkout-paypaliframe-load').innerHTML = iframedata;

                                }
                                else   //other payment action
                                {
                                    this.isSuccess = true;
                                    window.location = data.success;
                                }
                            }
                        }
                    },
                onFailure: checkout.ajaxFailure.bind(checkout)
            }
            );
    //var updater = new Ajax.Updater('product-details', this.saveUrl, {method: 'post',parameters: Form.serialize(this.form)});
    }
},

If I simply change the onclick to setTimeout it doesn't work.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jose Luis
  • 13
  • 5
  • 1
    instead on calling onClick you can directly use setTimeout(review.save(),10000); use this it will call function after some interval for more reference http://stackoverflow.com/a/11901175/4944490 – Krupesh Kotecha Oct 09 '15 at 11:53

1 Answers1

3

Use setTimeout in your javascript file.

Second parameter is time in milliseconds (1000ms = 1s), after which function will be executed.

setTimeout(review.save, 1000);

EDIT:

Sinde you use this in your function, you need to overwrite this. If called independently, scope isn't same anymore.

setTimeout(function(){
    review.save.apply(document.getElementById('review-btn'));
}, 1000);

Full code

Add this to last row of your JS file.

window.onload = function(){
    setTimeout(function(){
        review.save.apply(document.getElementById('review-btn'));
    }, 1000);
};
Rene Korss
  • 5,414
  • 3
  • 31
  • 38