3

I want to be able to run this script more than once without reloading the page. Have looked att using Live events, but couldn't figure it out. Any help would be greatly appreciated. Btw, I'm a noob and I haven't written the script myself.

<script type="text/javascript">

var $elem = $('#wrapper');
$(document).ready(function(){
    $("a#trigger").click(function(event){
        event.preventDefault();
        var full_url = this.href;
        var parts = full_url.split("#");
        var trgt = parts[1];
        var target_offset = $("#"+trgt).offset();
        var target_top = target_offset.top;
        $('html, body').delay(2000).animate({scrollTop:target_top}, 2000).delay(250).queue(function() {
            $('#arm').hide();
            $('#arm').toggleClass('arm-down');
        });
    });
    $(function(){
        $('#arm').hide();
        $('#arm').toggleClass('arm-down');
        $('a#trigger').click(function() {
            $('#trigger').addClass('active');
            $('#arm').delay(500).slideToggle().delay(750).queue(function() {
                $('#arm').toggleClass('arm-grab');
            });
        });
    });
});
</script>
David Ashman
  • 31
  • 1
  • 2

4 Answers4

1

Lets assume you dont want to set the click handler a bunch of times. That just leaves the anonoymous function.

Step 1. !anonymous

Change the anonymous function into a not-anonymous function.

function blammo(triggeringEvent)
{
    $('#arm').hide();
    $('#arm').toggleClass('arm-down');
    $('a#trigger').click(function()
    {
        $('#trigger').addClass('active');
        $('#arm').delay(500).slideToggle().delay(750).queue(function()
        {
            $('#arm').toggleClass('arm-grab');
        });
    });
}

Step 2. go blammo

Use the not-anonymous function and use the .on() jQuery function.

$(document).ready(function()
{
  ... blah ...
  blammo(null); // instead of the anonymous function.

  $(something).on("some event, maybe click", blammo);
}
DwB
  • 37,124
  • 11
  • 56
  • 82
  • Easier to write, sure. But there's no functional change from doing it this way. – Anthony Grist Feb 24 '12 at 16:57
  • If by "no functional change" you mean that the function is now, also, called in response to the event (as setup by the .on()) then I agree. Otherwise, I must disagree. The addition of the .on() is new functionality. – DwB Feb 24 '12 at 19:02
  • @DwB - Thanks! Total noob, sorry for that. But don't understand step 2. What is blah suppose to include instead? And what about the top part of the script, row 1-14? Thanks again! – David Ashman Feb 26 '12 at 20:10
  • blah is the top part of the script. – DwB Feb 27 '12 at 15:07
1

Make your anonymous function:

function(event){
        event.preventDefault();
        var full_url = this.href;
        var parts = full_url.split("#");
        var trgt = parts[1];
        var target_offset = $("#"+trgt).offset();
        var target_top = target_offset.top;
        $('html, body').delay(2000).animate({scrollTop:target_top}, 2000).delay(250).queue(function() {
            $('#arm').hide();
            $('#arm').toggleClass('arm-down');
        });
    });
    $(function(){
        $('#arm').hide();
        $('#arm').toggleClass('arm-down');
        $('a#trigger').click(function() {
            $('#trigger').addClass('active');
            $('#arm').delay(500).slideToggle().delay(750).queue(function() {
                $('#arm').toggleClass('arm-grab');
            });
        });
    });
}

into a function

function yourFunction(event)

which reduces your onLoad to

$(document).ready(yourFunction)

now you can call your function whenever you want

<script>
//call your function
yourFunction(null)
</script>
Paul Nikonowicz
  • 3,883
  • 21
  • 39
  • Thanks for answer, but being a total noob, I don't fully follow your fix. Don't know exactly how to make it into a var. Sorry. Could you please be more exact in the steps? Thanks! – David Ashman Feb 26 '12 at 20:19
  • Don't focus on the var part. i made an edit and changed the var keyword to function. – Paul Nikonowicz Feb 27 '12 at 14:34
0

If you want any chunk of js to run more than once, put the whole thing in a function, then use setTimeOut() in it to call itself.

Abhranil Das
  • 5,702
  • 6
  • 35
  • 42
0

Have no idea what is going on here, but I'll try anyway :-)

<script type="text/javascript">
$(function(){
    $('#arm').hide().toggleClass('arm-down');
    $('#trigger').on('click', function(event) {
         event.preventDefault();
         var hash = this.href.split("#"),
             target_top = hash[1].offset().top;
         $(this).addClass('active');
         $('#arm').delay(500).slideToggle().delay(750).queue(function() {
             $(this).toggleClass('arm-grab');
         });
         $('html, body').delay(2000).animate({scrollTop:target_top}, 2000).delay(250).queue(function() {
             $('#arm').hide().toggleClass('arm-down');
         });
    });
});
</script>
adeneo
  • 312,895
  • 29
  • 395
  • 388