To do this we will be using the normal setInterval in jQuery. We will create an element to be considered to be our container div. And by using setInterval, we will refresh the content that we created dynamically to our container div. I hope you will like this.
Please see this article in my
blog.
Using the code
To work with it, load your jQuery reference first. I am using the following version of jQuery.
- <script src="http://sibeeshpassion.com/content/scripts/jquery-2.0.2.min.js"></script>
Create a container div
- <div id='container'></div>
So now we can do our script part.
Implementation
Here's the scripts:
- $(document).ready(
- function() {
- setInterval(function() {
- var randomnumber = Math.floor(Math.random() * 20);
- var html='';
- for(i=0;i<randomnumber;i++)
- {
- html+='<p>'+ randomnumber+ '</p>';
- }
- $('#container').html(html);
- }, 1000);
- });
As you can see, we are creating some random numbers less than 20.
- var randomnumber = Math.floor(Math.random() * 20);
And assigning the random value to the dynamically created p tag.
- html+='<p>'+ randomnumber+ '</p>'
Now we need to style our p tag as in the following:
- p {
- border: 1px solid #ccc;
- border-radius: 5px;
- height: 18px;
- width: 18px;
- padding: 5px;
- text-align: center;
- background-color: #999;
- color: #fff;
- float: left;
- }
That is all, it is time to run our code.
jsfiddle link: Loading or refreshing a div content jsfiddle
Output
Conclusion
I hope you liked this article. Please share with me your valuable feedback. Thanks in advance.