1

I have used the twitter widget to display my timeline on a simple webpage. But what I really want to achieve is , whenever I tweet something from twitter ,I want to see it on the webpage I have created without being refreshed. So what I want to do is call the script after every 3secs or so. Please let me know how to do it.

Here's the code for the html page I have done.

<html>
<head>
<title>Twitter Feed Test</title>

</head>
<body>
<p>Testing how to get the the twitter feeds to webpage</p>
<a class="twitter-timeline" href="https://twitter.com/happyman_mayur" data-widget-id="509282432604844032">Tweets by @happyman_mayur</a>
<script>
!function(d,s,id){
var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';
if(!d.getElementById(id))
{
js=d.createElement(s);
js.id=id;js.src=p+"://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js,fjs);}
}(document,"script","twitter-wjs");
</script>
</body>
</html>
Mayank
  • 165
  • 1
  • 5
  • 20
  • [setInterval](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.setInterval) – Collett89 Sep 09 '14 at 10:50
  • 2
    `setTimeout()` would seem appropriate here. – George Sep 09 '14 at 10:51
  • possible duplicate of [Is there some way to introduce a delay in javascript?](http://stackoverflow.com/questions/24849/is-there-some-way-to-introduce-a-delay-in-javascript) – Sebastien C. Sep 09 '14 at 10:54
  • The problem so far with the answers is that while they mention `setInterval` or `setTimeout`, the script in the question is just the script that is embedding the widget. You don't want to put that code in `setInterval` because it won't work. It is checking that there is no existing element with id "twitter-wjs", if it does, it does nothing. – Cᴏʀʏ Sep 09 '14 at 10:56
  • Twitter widgets poll for updates automatically every 1 second as long as you have not defined a tweet limit. Are you not seeing it refresh? – Cᴏʀʏ Sep 09 '14 at 10:58

2 Answers2

0

There might be a better, Twitter-specific way, but the simplest way to do so in Javascript is to use the setInterval function.

setInterval(function () {
    // Your code here
}, 3000)

More info

Tom
  • 522
  • 3
  • 13
  • Not able to get it work, can you try it on JSFiddle and help me please – Mayank Sep 09 '14 at 11:03
  • http://jsfiddle.net/gqsqzxu3/ As @Cory pointed out, the code simply creates the element if it isn't already there, so it requires a bit of fiddling. However, he also said that there's an automatic refresh, so maybe this isn't necessary? EDIT: tis solution won't work because it doesn't clear the old widget. I'll fix it if it turns out that Twitter's widget doesn't auto-refresh. – Tom Sep 09 '14 at 11:07
0

With setInterval or setTimeout (recursive);

You should visit http://www.w3schools.com/jsref/met_win_setinterval.asp

But be careful to clearInterval o clearTimeout on unload

laruiss
  • 3,780
  • 1
  • 18
  • 29