开发者

Getting file every 5 sec with toggle and idle check doesn't stop

开发者 https://www.devze.com 2023-02-10 23:22 出处:网络
var timeoutId; function getenginedetails(){ $.get(\'engine.php\', function(enginedata) { $(\'#detailsoutput\').html(enginedata);
var timeoutId;

function getenginedetails(){
    $.get('engine.php', function(enginedata) {
        $('#detailsoutput').html(enginedata);
    })

    timeoutId = setTimeout(getenginedetails, 5000);
}

getenginedetails();

$(document).ready(function(){   
    $('#pause').toggle( function() {
        clearTimeout(timeoutId);
        ('#pause').attr({ value: "Start" });
    }, function() {
        getenginedetails();
        ('#pause').attr({ value: "pause" });
    });
});

$.idleTimer(60000);

 $(document).bind("idle.idleTimer", function(){
    clearTimeout(timeoutId);
    ('#pause').attr({ value: "Start" });
 });

Hi, I开发者_StackOverflow'm using the jQuery idle timer plugin ( http://paulirish.com/2009/jquery-idletimer-plugin/ ) to check if the user wasn't idle for 60 seconds. If he is idle it stops getting the engine.php file. Also I wanna add a pause button, and if the user was idle put the game automaticly on pause. But the whole thing doesn't work. It only gets the file and when I press the button the file comes in again.


Edit:

@Mash:

var paused = false;

function getenginedetails(){
    $.get('engine.php', function(enginedata) {
        $('#detailsoutput').html(enginedata);
    })

    if (!paused){
        timeoutId = setTimeout(getenginedetails, 5000);
    }
}

function setpause(){
    paused = true;
    ('#pause').attr({
        value: "  Haal van pauze af  ",
        onclick: "setnonpause();"
    });
}

function setnonpause(){
    paused = false;
    ('#pause').attr({
        value: "  Zet op pauze  ",
        onclick: "setpause();"
    });
}

$.idleTimer(60000);

 $(document).bind("idle.idleTimer", function(){
    setpause();
 });

getenginedetails();

This doesn't work neither. I get an error at ('#pause').attr({?


In your getenginedetails() function, you should check if it's paused, of so, do not call the setTimeout(getenginedetails, 5000)
For this, you will have to add a boolean variable like:

var paused = false;

and the function will look like this:

function getenginedetails(){
    $.get('engine.php', function(enginedata) {
        $('#detailsoutput').html(enginedata);
    })

    if (!paused){
        timeoutId = setTimeout(getenginedetails, 5000);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消