Show something before setInterval jquery - php

my question is how can i show instant some information before load setInterval in jquery, and when is load setInterval will update it.
<script type=\"text/javascript\">
function getData(){
$(\"#whereshow\").load(\"somefile.php\");
}
setInterval(\"getData()\", 5000);
</script>
Can i make something like that?
<script type=\"text/javascript\">
function getData1(){
$(\"#whereshow\").load(\"somefile.php\");
}
function getData(){
$(\"#whereshow\").load(\"somefile.php\");
}
setInterval(\"getData()\", 5000);
</script>
...and getData1() will display instant onload page info, then will stop, and setIntervall wil do the other update job?

Basically what you want to do is have the setInterval fire immediately as well as on the interval.
That's easy.
(function() {
var getData = function() {
$("#whereshow").load("somefile.php");
}
setInterval(getData,5000);
getData();
})();
See, just call the function ;) I also added some improvements to your script.
EDIT: Another way of doing it that I just thought of:
setInterval((function() {
$("#whereshow").load("somefile.php");
return arguments.callee;
})(),5000);

Related

Jquery reload every second while scrolled down in the beginning

I am trying to make a div reload every second and to have it scrolled down in the beginning. Right know I put both in the same function, because scrolling script doesn't work when they are apart (because of the reloading). However, in this case it scrolls down every second. Is there a way to have the div scrolled down only in the beginning and reload it every second? Thanks!
<script>
function load(){
$('#screen').load('includes/update.php');
$("#screen").scrollTop($("#screen")[0].scrollHeight);
}
setInterval(function(){
load();
}, 1000);
</script>
You could have an outher variable that says if you have scrolled or not :
<script>
var scrolled = false;
function load(){
$('#screen').load('includes/update.php');
if(!scrolled){
$("#screen").scrollTop($("#screen")[0].scrollHeight);
scrolled = true;
}
}
setInterval(function(){
load();
}, 1000);
</script>
This way, load will be called every second, but since scrolled is in the outer scope, it will be the same variable for each call.
You have to use the complete callback function for .load() like this:
<script>
function load(){
$('#screen').load('includes/update.php', function(){
$("#screen").scrollTop($("#screen")[0].scrollHeight);
});
}
setInterval(function(){
load();
}, 1000);
</script>
And it means that when the content is loaded fully, then call the callback functions and scroll to the top.
For more reference you can use the jQuery .load() documentation

Confirmation Before Closing a Popup Window?

I have developed the code for a popup window after clicking on a link. But what I want is to show a confirmation alert before closing the popup window.
How can I achieve this?
My code:
<script type="text/javascript">
$(document).ready(function(e) {
window.onload=test_create();
function test_create()
{
alert('Test Function');
}
});
</script>
<script type="text/javascript">
$(document).ready(function(e) {
$(body).unload(function()
{
alert('Close');
});
});
</script>
This should do:
$(window).on('beforeunload', function() {
return confirm("Are you sure you want to leave?");
});
try this:
$(window).on('beforeunload', function(e) {
alert('Close');
});
or if you want to make a question if the user want really to close the page you can try this:
$(window).on('beforeunload', function(e) {
if(confirm("Are you sure you want to close this window")){
return true;
}else{
return false;
}
});
Try these way
Javascript:
window.onunload = function(){
//Do something
}
Jquery:
$(window).unload(function(){
//Do something
});
The best way to create popups on JavaScript in my opinion is based on create div elements
dinamically for have you'r on popups withs you'r own values and styles.
Anyway I think you are looking for somting like JavaScript prompt(), take a look to documentation. There you have an fiddle exemple.

PHP Update using Javascript

I have my php file which contains my method to update the database. However, in Javascript how do I make it so every 5 seconds say it "visits" this page so it's contents gets updated.
Here is my update.php file:
<?php include('config.php') ?>
<?php
mysql_query("UPDATE paint SET paint_points='test'") or die(mysql_error());
echo "Updated";
?>
Sorry, I'm not familiar with the terminology.
Thanks
Use the setInterval function with an (a)jax request every 5 secs in javascript:
//syncronized jax:
function myjax() {
var oXhr = new XMLHttpRequest();
oXhr.open("POST", "yourphp.php", false);
oXhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
oXhr.send(null);
}
//set an interval each 5 seconds to call your myjax method
setInterval(function() { myjax(); }, 5000);
In this example the request is synchronous but it could be asynchronous if you wished so.
The simplest case is to reload the page with:
<script type="text/javascript">
setInterval(function() { location.reload(true); }, 5000);
</script>
You can get fancier if you use an ajax call to fetch the page.
Using jQuery:
(function() {
var updateAgain = arguments.callee;
$.get('/url/to/script.php', function() {
setTimeout(updateAgain, 5000);
});
})();
The advantage of this over setInterval is that it won't start counting to five seconds until the request is finished; this is important if the request takes more than a second or two. It will also stop if a request fails (which may or may not be an advantage).

Load javascript function and when its loaded load it again in a specified number of seconds

He guys,
Maybe you can help me, i need a code when the page is loaded a function (below)
function update() {
$('#animation').load('/Stream/readLevel');
}
needs to execute, when this action is complete (so the content of the page /Stream/readLevel has been fully loaded the function must be loaded again in a number of seconds (2 seconds).
How can i accomplish this?
Thanks in advance!
As you only want to reload after the contents has been fully loaded you have to set the timer in success callback of $.load function like following, otherwise it will send request to server every two seconds irrespect of if the previous ajax request has been completed or not.
function update() {
$('#animation').load('/Stream/readLevel',function(){
var timer = setTimeout(update,2000);
});
}
$(function(){
update();
});
you can do it like this.
$(function(){
update();
});
function update() {
$('#animation').load('/Stream/readLevel', function(){
alert("after two seconds of load");
setTimeout('update()', 2000);
});
}
​
<script>
function update() {
$("#animation").load('/Stream/readLevel',"",function(){
setTimeout(update,2000);
});
}
</script>
<body onload="update()">
// HTML CODE
</body>
Try
function update(){
$('#animation').load('/Stream/readLevel',function(){
setTimeout(update,2000); });
}
var c = 2;
function update() {
$('#animation').load('/Stream/readLevel');
}
function startTimer(){
if(c< 0){
c=2;
}
if(c==2){
update();
}
timer = setTimeout('startTimer()',1000);
c=c-1;
}
$(document).ready(function(){
startTimer();
});

javascript function not working after page load

why my code is not working? I have called a javascript function after page load PHP script.like below:
<script>
setTimeout("fb_login()",100);
</script>
fb_login() function is on same page
function fb_login()
{
alert("ok");
}
Tried with setTimeout("fb_login",100); too. but not working.
I have checked console, but it's giving no error.
Just change it to:
<script>
setTimeout(fb_login, 100);
</script>
Change this code:
<script>
setTimeout("fb_login()",100);
</script>
to this:
<script>
setTimeout(fb_login,100);
</script>
Good explanation from similar post - How can I pass a parameter to a setTimeout() callback?
It might be you given less time in setTimeout but and it's calling your function befor page loaded fully. So Try to increase time.
<script>
setTimeout("fb_login()",1000);
</script>
Make sure that fb_login is being initialized before calling otherwise it will give error. Either use document.ready or add that function before it is called. Does it give you some error like "fb_login is undefined" ?
try this
<script>
window.onload = function(){
setTimeout(fb_login,100);
};
function fb_login(){
alert("ok");
}
</script>
EDIT: first check if the below is working or not, if it does not work then pbm is somewhere else
window.onload = function(){
setTimeout(function(){
fb_login();
},100);
};

Categories