Search code examples
jqueryasp.net-mvcazureknockout-mvc

Using Jquery to update an Azure web page


I am trying to build a website in MVC 4.0 and knockoutMVC that uses jQuery to update elements on a page, the website will be run from Azure web, the code I have works fine when I run it locally, however when I upload to azure no updates happen on the view. Is there any reason for this to happen. I am using the free Azure trial currently.

I have pulled all the code out and left myself with just a hello world page that will update the time on the page every 4 seconds. However even this won't work.

<script type="text/javascript">
var myVar = setInterval(function () { UpdateTime(); }, 4000);

$(document).ready(function UpdateTime() {
    var now = new Date();
    $("#time").text(now.toLocaleTimeString());
});
</script>
<h2>HelloWorld</h2>
<label id="time"></label>

Solution

  • Somehow, it seems to work locally, but only updates the time once. I'm not exactly sure why it even works on the first hit, so maybe someone else could add more here on that. I would recommend defining the update function outside of the document ready event, and only setup the setInterval at that point, when your element is guaranteed to be loaded. You could also avoid finding the element every four seconds as well. Not sure if that answers your question, but it didn't fully work for me locally.

    var myVar, $time;
    function UpdateTime() {
        var now = new Date();
        $time.text(now.toLocaleTimeString());
    }
    $(document).ready(function(){
        $time = $("#time");
        myVar = setInterval(UpdateTime, 4000);
    });