简体   繁体   中英

php value refreshes every 10s but I want it to load instantly with page

I'm currently using the following code to push the last bitcoin price into the placeholder field of an input textbox every 10 seconds:

var auto_refresh = setInterval(
 function()
 {$.get('gox.php', function (data) {
      $("#inputid").attr('placeholder', data);
 });}, 10000);

gox.php outputs a value (eg 99.9999)

The problem is when I load my page the placeholder remains blank (since I set placeholder="" in the html) and doesn't refresh to the bitcoin price until 10 seconds have passed (the 10000 you see in the code).

I have tried:

function()
 {$.get('gox.php', function (data) {
      $("#inputid").attr('placeholder', data);
 });}

But it doesn't work.

My goal is to have the bitcoin price populate the placeholder value as soon as the page is loaded and then refresh it every 10 seconds.

Thanks in advance for your help!

var refreshBitCoin = function() {
    $.get('gox.php', function (data) {
        $("#inputid").attr('placeholder', data);
    });
}

At the bottom of the page (or within a jquery ready block)

refreshBitCoin(); 
setInterval(refreshBitCoin, 10000);

Create an external function

function page_info(){
    $.get('gox.php', function (data) {
        $("#inputid").attr('placeholder', data);
    });
}


$(document).ready(function(){

    page_info(); // refresh
    var auto_refresh = setInterval(page_info, 10000);

});
function updateBitCoin(){

 $.ajax({
        'url' : 'gox.php',
        'type' : 'GET',
        success : function(data){
            setTimeout('updateBitCoin()', 10000);
                            $("#inputid").attr('placeholder', data);
        }
    }) 
}

$(updateBitCoin);

That will fire the function at the begining and continue it every ten seconds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM