Jump to content

jQuery while-loop

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Takumi

Takumi

    Newbie

  • Members
  • PipPip
  • 16 posts

Quote

Hi Coders,
I got a problem with the jQuery while loop and variables. I am making a script for a map, where users can scroll over. The moving script is working perfectly but I need now a jQuery event or JavaScript function to run it correctly. I am now running the script with .mousemove(), this script will only be run when the mouse is moving. I need to run it when a global variable = true. For this I need a while loop. But somehow this isnt working.

Click on the following link to see a live example: Famares Map Test

Takumi

*SOLVED*

Edited by Takumi, 12 November 2010 - 10:34 AM.


#2
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Remove var from mouseenter and mouseleave handlers:
    $("#famaresgamemap").mouseenter(function() {
        live = true;
        $('span#leftmargin').text(i);
        $("span#live").text("live: " + live);
    }).mouseleave(function() {
        live = false;
        move = false;
        $('span#leftmargin').text(i);
        $("span#live").text("live: " + live);
        $("span#move").text("move: false");
    }); 
You can replace:
while (live == true)
with:
while (live)
If there is no special need you can replace this:
    if (move == 'undefined') {
        var move = false;
    } else {
        var move = move;
    }
    if (live == 'undefined') {
        var live = false;
    } 
with
var live = false, move = false;
To be honest I don't understand why you need here while loop.

#3
Takumi

Takumi

    Newbie

  • Members
  • PipPip
  • 16 posts

Vladimir said:

Remove var from mouseenter and mouseleave handlers:

    $("#famaresgamemap").mouseenter(function() {

        live = true;

        $('span#leftmargin').text(i);

        $("span#live").text("live: " + live);

    }).mouseleave(function() {

        live = false;

        move = false;

        $('span#leftmargin').text(i);

        $("span#live").text("live: " + live);

        $("span#move").text("move: false");

    }); 

You can replace:
while (live == true)
with:
while (live)
If there is no special need you can replace this:
    if (move == 'undefined') {

        var move = false;

    } else {

        var move = move;

    }

    if (live == 'undefined') {

        var live = false;

    } 
with
var live = false, move = false;
To be honest I don't understand why you need here while loop.

I have figured out that a while loop will not work, because it will execute too much at one time. I now replaced this with settimeout() (check first post).