Getting an AJAX call to reload a div every x minutes - javascript

I'm new to Javascript and AJAX so please excuse my newb question. I have an AJAX call (that is working) and I'd like to have it refresh the contents of a div every 5 minutes (I'm not using jquery). This is how I'm calling the AJAX function in the <head></head> of my html page:
<script type=text/javascript>
setInterval(ajaxCall(), 300000);
</script>
The initial page load populates the div, but the content of the div doesn't refresh after 5 minutes. What am I doing wrong?

Change your code to this:
setInterval(ajaxCall, 300000);
To pass an argument:
setInterval(function(){ ajaxCall(someCoolValue); }, 300000);
Notice the lack of parentheses in ajaxCall. You want to pass the function itself, not call the function. More examples on MDN.

You need to wrap your ajaxCall.
<script type=text/javascript>
setInterval(function(){ ajaxCall(); }, 300000);
</script>

Here
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
</head>
<div id="links">
</div>
<script language="javascript" type="text/javascript">
var timeout = setTimeout(reloadChat, 300000);
function reloadChat () {
$('#links').load('test.php #links',function () {
$(this).unwrap();
timeout = setTimeout(reloadChat, 300000);
});
}
</script>
Also you are calling it wrong correct syntax would be ,
setInterval(ajaxCall, 300000);
Hope it helps :D

Related

why refresh html page is slow

For some reason the method display is being processed before location.reload(). Here is my syntax:
$(document).ready(function() {
$("#refresh").click(function() {
alert("before refresh")
location.reload();
display()
})
})
function display() {
alert("hello world")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="refresh">
refersh and continue
</button>
My goal here is to refresh the page when the button is clicked then call display method.
The problem is that display method is being called before the page is refreshed.
First of all, is what I am trying to achieve achievable. If not, what other ways can go take in order to solve the problem?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
display();
$("#refresh").click(function(){
alert("before refresh")
//location.reload();
display();
});
})
function display(){
alert("hello world")
}
</script>
<button id="refresh">
refersh and continue
</button>
</body>
</html>
Try this, call the display method on loading the document and remove the reload(), if needed add display() in refresh.
You could try assigning your display method to the document.onload event. Try changing your click handler to:
alert('before refresh');
$(document).on('load', display);
location.reload;

How to real time data after every second?

I want to show real time data, currently i used this code
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(function (){
$('#load_updates').load('comment.php').fadeIn("slow");
}, 10000); // refresh every 10000 milliseconds
</script>
<head>
<body>
<div id="load_updates"></div>
</body>
Now everything is fine, this code is working and shows data from comment.php, but the problem is that it refreshes data after 10 seconds and it's too much time. I tried to reduce the milliseconds but it's not working
There are some technologies to build a real time application. You can use WebSockets for example.
This link has some options:
https://entwickler.de/webandphp/building-realtime-web-apps-with-php-125787.html

How to submit a form when page loads using JavaScript?

I know this question has been answered many times but in my case the JavaScript code creates a infinite loop which makes my page load continuously,
<script type="text/javascript">
window.onload = function(){
document.getElementById('theForm').submit();
}
</script>
I also tried the below one and it doesn't work:
<body onload="document.theform.submit()";>
The below code does what I need but it's not perfect:
<script type="text/javascript">
window.onload = function(){
document.getElementById('submit_button').click();
setTimeout(function(){window.stop();},2500);
}
</script>
If your goal is execute submit onload only 1 times and then break loop, you must add to form some hidden variable and increase variable on every submit. And then on onload function check with if statement if hidden variable larger then 2 then do not submit anymore.
Try Below Code
<script type="text/javascript">
$(document).ready(function(){
document.theForm.submit();
});
</script>
$(document).ready(function() {
while (1) {
$("#theForm").submit();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form method="post" id="theForm" name="theForm">
</form>

setTimeout for delaying opening a popup in Javascript

I need for delaying opening of a popup.
This is the code of my popup:
<script type="text/javascript">
var pmauid = 'Numbers';
var pmawid = 'Numbers';
var fq = '0';
</script>
<script type="text/javascript" src="http://mypopnetwork.com"></script>
So, I need for apply a delay in this part of code:
<script type="text/javascript" src="http://mypopnetwork.com"></script>
If I editing in this way, nothing happened:
<script type="text/javascript" window.setTimeout() src="http://mypopnetwork.com"></script>
Maybe I wrong syntax. This popup is normally open after clicking on an alertbox.
I tried also in this way:
setTimeout(function () {
<script type="text/javascript" src="http://mypopnetwork.com"></script>;
}, 5000);
For one, you are treating JavaScript as if it were some sort of attribute, It should be in between the <script> tags (if your doing it inline) like:
<script>
// your code here
</script>
or if you want to reference a JavaScript file:
<script src="./whatever.js"></script>
or
<script src="http://foo.com/script.js"></script>
Second off, you need to provide a callback and a timeout length to setTimeout(). So in the end, you code should look like:
setTimeout(function () {
alert("whatever");
}, 5000);
Where the callback is what you want to fire after the timer and 500 is the delay in milliseconds.

How to run JavaScript code before page load?

I have working in asp.net web application. Here I need to run JavaScript before page load.
I have tried:
<body oninit="funinit();" onprerender="funRender();" onload="funload();">
</body>
<script type="text/javascript" language="javascript">
function funinit() {
alert("funinit");
}
function funload() {
alert("funload");
}
function funRender() {
alert("funRender");
}
</script>
here only funload() is working.
You can use window.onpaint for such purpose like :
<script type="text/javascript">
function preloadFunc()
{
alert("PreLoad");
}
window.onpaint = preloadFunc();
</script>
I hope it helps you....
Just inline it?
<script type='text/javascript'>
alert("funload");
</script>
Or put it in a function and call it immediately. Try to put it to the topmost of your page, however since the DOM isnt loaded yet you cant get any other elements.
What is it you want to do?
just insert a <script> tag wherever inside the body you want it to run. it will be executed as soon as the parser reads it, as long as it doesn't reference an element not yet created
try to put your script in head section of the page:
<head>
<script type="text/javascript" language="javascript">
alert("funinit");
alert("funRender");
</script>
</head>
Why not Use the ClientScriptManager.RegisterClientScriptBlock Method
http://msdn.microsoft.com/en-us/library/btf44dc9.aspx

Categories