Krishna Thota
Krishna Thota

Reputation: 7066

Returning Value in JavaScript callback for jQuery

Sorry I could not find the appropriate title

Here is my problem

I am using a method which returns a string value like hh:mm(12:45) this method is named as DeliveryTimeCalc()

I am using a jQuery timepicker to take input on my aspx page this timepicker has to be validated by mintime the minimum time should be the value returned by the method DeliveryTimeCalc() this serverside method has to be called during the jQuery is initialized so I did the below method

<script type="text/javascript">
    $(document).ready(function () {
        //window.onload = pageMethodConcept.callServerSideMethod;
        var time = pageMethodConcept.callServerSideMethod;
        alert(time);
        var hm = time.split(':');
        var h = hm[0]; var m = hm[1];
        $("#tb_DeliveryTime").timepicker({ showPeriodLabels: false,
            onHourShow: OnHourShowCallback,
            onMinuteShow: OnMinuteShowCallback
        });
        function OnHourShowCallback(hour) {

            if ((hour < h)) {
                return false; // not valid
            }
            return true; // valid
        }
        function OnMinuteShowCallback(hour, minute) {
            if ((hour == h) && (minute <= m)) { return false; } // not valid
            return true;  // valid
        }
    });
</script>
<script language="javascript" type="text/javascript">
pageMethodConcept = {
    callServerSideMethod: function () {
         PageMethods.DeliveryTimeCalc(pageMethodConcept.callback);
    },
    callback: function (result) {
        alert(result);
        //return result;
        }
    }
    //window.onload = pageMethodConcept.callServerSideMethod;
</script>

but the problem is that it is not returning the value (hh:mm) I am getting a alert box which contains output like

function () {
    PageMethods.DeliveryTimeCalc(pageMethodConcept.callback);
}

even if I use return I am getting the same value

but if I use

window.onload = pageMethodConcept.callServerSideMethod;

I am getting a alert box which contains output like hh:mm

Please Help!

Upvotes: 0

Views: 298

Answers (2)

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123407

Try simply changing:

var time = pageMethodConcept.callServerSideMethod();

with trailing () : you have to assign the return value of the function to the variable, not the function itself, but:

window.onload = pageMethodConcept.callServerSideMethod;

works as expected because you're assigning an handler to an event and you have no parameters to pass along with the function: this in fact it could be written as well as:

window.onload = function() {
    pageMethodConcept.callServerSideMethod();
}

Upvotes: 1

Krishna Thota
Krishna Thota

Reputation: 7066

Thank you guys for helping but i have found the solution myself

Sol 1 Using Web Services

http://markitup.com/WebServices/TimeZones.asmx?op=CurrentDateTime

this is a web service in which we have to enter the name of the time zone Ex:(India Standard Time)

Sol 2

Write a method in the pageload and in that method write your code. When you have to return the result place the result in a labe text and make the visibility of the label to false (Note: You can not find the label in javascript if the visibility is set to false So do this instead of making visibility false. style="display: none;" this does the same but in JavaScript you can find the label) you can find the label in javascript by using

var time = $("#lbl_time").text();

Since we coded the jquery as $(document).ready(function () the jquery will be executed only after the page load is executed

So Problem Solved

Thank You

-Krishna Thota

Upvotes: 0

Related Questions