How to initialize a blocking function on page load

I have the following

$(document).ready(function () {
    //window.onload = LabourPrice;

    //Control Proofing Time and LabourCost
    $('#ArtworkDetail_NoOfProofs').keyup(function () {
        function LabourPrice() {
            var time = "@Model.ArtworkDetail.ProofType.DefaultProofTime".split(':');
            var seconds = (+time[0]) * 60 * 60 + (+time[1]) * 60 + (+time[2]);
            var newSeconds = seconds * $('#ArtworkDetail_NoOfProofs').val();

            var date = new Date(newSeconds * 1000);
            var hh = date.getUTCHours();
            var mm = date.getUTCMinutes();
            var ss = date.getSeconds();

            var hourlyLabour = $('#LabourCostCentrePrice').val();
            hourlyLabour = hourlyLabour.split('£');
            var costPerSecond = hourlyLabour[1] / 3600;
            var calculateCost = costPerSecond * newSeconds;
            //alert("£"+calculateCost.toFixed(2));
            $('#ArtworkDetail_ProofingLabourCost').val("£" + calculateCost.toFixed(2));

            // If building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time
            if (hh > 12) { hh = hh % 12; }
            // Ensure each element has two-digits
            if (hh < 10) { hh = "0" + hh; }
            if (mm < 10) { mm = "0" + mm; }
            if (ss < 10) { ss = "0" + ss; }
            // Format your to HH:MM:SS
            var t = hh + ":" + mm + ":" + ss;
            $('#ArtworkDetail_ProofType_DefaultProofTime').val(t);
        }
        });

});

      

I would like to call the keyup function on page load to trigger some kind of default that I have when calculating, since the price starts out as empty at first.

Can I do something like this?

$(document).ready(function () {
   window.onload = LabourPrice;
    ....
  }

      

Then how would I wrap LabourPrice?

thank

EDIT

Added part function as requested

+3


source to share


2 answers


I find this to window.onload

be redundant as it is part ready

and the event must be set inside the function ready

. Why not try

$(document).ready(function () {
    $('#ArtworkDetail_NoOfProofs').keyup(function () {
    });
    LabourPrice();
}
function LabourPrice() {
}

      



Assuming which LabourPrice

is a function (just saw your edit). This function definition can go beyondready

+2


source


LabourPrice

must be outside the event keyup

. The way you do it now LabourPrice

only exists on the keyup event, but you want to call it from the keyup event:

$(window).load(function(){
  $('#ArtworkDetail_NoOfProofs').keyup(function () {
    LabourPrice();
  }
}

function LabourPrice(){ ... }

      



There's a good source here on how window.load differs from document.ready, although that's not your main problem.

+2


source







All Articles