Right now, I am working with the Tumblr API (v2) and trying to accomplish something I have never done before. I am a beginner with Javascript and tried to find a solution for my problem, but after several hours of trial and error I wasn’t able to solve it.
Anyway – This is the code I am using:
<script>
$.ajax({
type: 'GET',
url: 'https://api.tumblr.com/v2/blog/sitename.tumblr.com/posts/video?api_key=APIKEY&tag=featured',
data: {
get_param: 'value'
},
dataType: 'jsonp',
success: function(data) {
for (i = 3; i <= 6; i++) {
var time = moment.unix(data.response.posts[i].timestamp).format("MMMM DD, YYYY");
$('#topvid').append('<div class="uk-width-1-2 uk-width-1-4@s">\
<div class="cm-topfour"><div class="uk-inline-clip uk-transition-toggle uk-light">\
<img src="'+ data.response.posts[i].thumbnail_url +'" alt="">\
<div class="uk-position-center uk-text-center"><a href="" class="uk-icon-link uk-transition-fade" uk-icon="icon: play-circle; ratio: 3.0"></a></div>\
<div class="uk-overlay uk-position-top cm--catflag"><span>'+ data.response.posts[i].tags[0] +'</span></div>\
<div class="uk-overlay uk-position-bottom cm--itemfooter">\
<div class="uk-float-left">'+ document.write(time) +'</div>\
<div class="uk-float-right">Notes '+ data.response.posts[i].note_count +'</div>\
</div></div></div></div>');
}
}
});
</script>
What I am doing try to do here is the following: There is a value called timestamp within the json object, which is a Unix timestamp (e.g.: timestamp: 1512838102). To format the timestamp I am using moment.js. Moment.js is working fine, if I use it outside of the code above.
Next I did try to store the timestamp inside of a variable (time) and inside the loop. I am not even sure, if this is the right way. After I did this, I tried to output the var within the append-part. I tried several ways, but all I got so far was “undefined” or nothing.
I am sure, I am doing it all wrong and I did mess it up at some point. Again, what I am trying to is: Use the timestamp value with in the loop inside the moment.js script and print the final value within the append-part, so it is visible on the page for each iteration.
Thanks for your help in advance!
<div class="uk-float-left">'+ document.write(time) +'</div>because you are just printing a string...var time = moment.unix(data.response.posts[i].timestamp).format("MMMM DD, YYYY");, that I would be able to print it later. But maybe I am just mixing it up.varcan behave in an unintuitive manner when used inside closures created inside loops, so that's an obvious first thing to check for)..innerHTMLto display javascript between HTML tags. stackoverflow.com/questions/16467536/… Look at this question.