tags:

views:

2757

answers:

4

I'm making calls from managed code to javascript, and would like to display time (passed from managed code),

I've got a function such as:

function sometimefunction(sometime) {

    $(document).ready(function() {
        $('#currenttime').before($('<div class="timeupdate"></div>').html('<h3>Last update: ' + sometime+ '</h3')).remove();          
    })
}

Now, this replaces the time, but only on the first call..what I would like to happen is to replace time everytime this function is called...(btw. it's a timer, every second from managed code)

+3  A: 

I think the reason you are having the problem is because your function is just subscribing to a document load event.

You should be okay if you remove the document.ready call:

function sometimefunction(sometime) {
    $('#currenttime').before($('<div class="timeupdate"></div>').html('<h3>Last update: ' + sometime+ '</h3')).remove();
}

Additionally, you may be able to use the jquery replaceWith function instead.

bendewey
I've tried removing $(document).ready(function()....However that didn't help.I also tried something like this: $('#counterscurrenttime').replaceWith($('<div class="quick-timeupdate">' + currentTime + '</div>')); - the problem with replaceWith that I see now, is that it will work the first time, but once it "replaces" the #counterscurrenttime, the second time it won't be there..it would have to be almost something such as: replaceItself.
ra170
A: 

document.Ready event gets only called by the browser when all the DOM content has been loaded.

Dmitri Farkov
so what's the solution?I've tried removing the $(document).ready(function()...didn't help.
ra170
+2  A: 

You are removing the update right after you add it. I don't get your HTML structure but here goes:

var someTimeFunc = function(sometime){
    $('#currenttime').innerHTML('<h3>'+ sometime +'</h3>')
}
Dmitri Farkov
you forgot [0] $('#currenttime')[0].innerHTML and it needs to be =such as $('#currenttime')[0].innerHTML = ('<h3>'+ sometime +'</h3>')But sure, it does the job, so thanks for help. (or hint)
ra170
Sorry. Glad it works in the end :) you could escape the [0] by doing$('#currenttime').html() rather than innerHTML
Dmitri Farkov
A: 

the code $('#currenttime').before(...).remove() will remove #currenttime from the page every time. that's exactly what you're asking it to do...

so:

<div id="currenttime"></div>

becomes:

<div class="timeupdate"><h3>Last update: 00/00/00 00:00:00<div>

when what you probably need to end up on the page is the div with timeupdate class to also have the ID your code is looking for:

<div id="currenttime" class="timeupdate"><h3>Last update: 00/00/00 00:00:00</h3><div>

see how that solves your problem? the easiest way to get there is to start with this code on the page:

<div id="currenttime" class="timeupdate"></div>

and just use the html() function to replace the contents directly:

$('#currenttime').html('<h3>Last update: ' + sometime+ '</h3');

no before(), no remove(), no problem. :)

ifatree