0

I have the following inline javascript, im using a jquery countdown plugin to display time remaining.

this code exists with each "Comment" on the page, hence it is repeating multiple time on the page. How can I make this external? and avoid the repetition?

im using .nt mvc razor and attaching id.

<script type="text/javascript">
    $(function () {
    var dateLeft = new Date(@(item.UnixTicks));
    $('#countdown-@(item.ID)').countdown({until: dateLeft, format:'MS', onExpiry: liftOff, onTick: watchCountdown}); 

    function liftOff() { 
        alert('We have lift off!'); 
    } 

    function watchCountdown(periods) { 
        $('#monitor-@(item.ID)').text('Just ' + periods[5] + ' minutes and ' + 
            periods[6] + ' seconds to go'); 
    }
    });
</script> 

1 Answer 1

1

You can put the UnixTicks into an attribute in the comment, give all of the comments a class="comment", and loop over them:

$('.Comment').each(function() { 
    var dateLeft = new Date(parseInt($(this).attr('data-unixticks'), 10));
    ...
});
Sign up to request clarification or add additional context in comments.

2 Comments

And same with the item.ID attribute.. :)
If the #monitor- elements are nested inside the "main" div then it would be even better to give them class e.g. monitor then simple $(this).find(".monitor") will find it. Nice!

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.