2

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)

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
2

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>')
}

2 Comments

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)
Sorry. Glad it works in the end :) you could escape the [0] by doing $('#currenttime').html() rather than innerHTML
0

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

1 Comment

so what's the solution? I've tried removing the $(document).ready(function()... didn't help.
0

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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.