EzDevInfo.com

once

Once CSS library

How to print an alert view only once in my app?

I want to display a message to users if there is a new version of the app they're using, and to display this message at the app's startup, only once.

I've managed to get the app final version and the bundle's one, and comparing them to display or not an alert view to propose the new app. Now I just want to know how to never display this alert again when it has been done once.

Thanks for your help.


Source: (StackOverflow)

Changing volume for one alarm sound only on Android

I am working on a life saving medical app and if the user is in a life threatening situation, they need to hear the alert.

When I have a notification to the status bar or have a dialog appear for a critical message to the user, I need to get their attention. If the media volume or ringer volume is low or off, I want to override it for my alert only. I would prefer not to change the settings for the phone, just for my one sound that I want to play.

When I try:

AudioManager audioManager = (AudioManager)getSystemService(this.AUDIO_SERVICE);  
audioManager.setStreamVolume(AudioManager.STREAM_RING, 
    audioManager.getStreamMaxVolume(AudioManager.STREAM_RING), 0); 

This correctly sets the volume for my stream but has the side effect of changing the stream volume for everyone else.

Is there a way of setting the volume for one song only?


Source: (StackOverflow)

Advertisements

how to undo jQuery's one() using on()?

I have a click event that needs to be fired not more than once :

    $(".menuIco").one('click', function() {
        $(this).menuIcoClick($(this));
    });

...that leads to function :

$.fn.menuIcoClick = function(menuIco) {
     [...does some animations, not important...]
    }

However, after other event I would like that event to be able to fire up again. I'm trying to bind menuIco's click event with menuIcoClick function again using on() :

.on('click', $('#analiza').menuIcoClick($('#analiza')));

in

$("#icoBack").click(function() {
        $('#analiza').animate({left: ($("#analiza").data("left")), top: ($("#analiza").data("top"))}, 0).on('click', $('#analiza').menuIcoClick($('#analiza')));
    });

but it doesn't work, on() method doesn't let me click (and fire up once-only event) on that element again, as I expected.


Source: (StackOverflow)

Pre-determined message types out on key press

I have a div that is contenteditable with the id of "editable." I want the end result to be that a user types, but rather than the physical key event, a pre-determined message appears.

For example, if the pre-determined message is the word, "Sing", then no matter what letter the user types first, the first letter to appear is "S", on the second key press, an "i" would appear, and so on.

I've found code on this forum that simulates changing the character associated with a key event, and I've modified the code to achieve the "S" from my example. I've pasted the code below and created it here: http://jsfiddle.net/Ukkmu/61/

This code, of course, gives me the "S" infinite times. Where I'm stuck is performing this function only once, then calling another function for the "i", then the "n" and so on.
I've gotten one possible lead on a toggle function that I suppose may work, but have been unable to discern how to apply it to my code.

function transformTypedCharacter(charStr) {
  return /[a-z]/.test(charStr) ? "S" : /[A-Z]/.test(charStr) ? "S" :   charStr;
}

function insertTextAtCursor(text) {
  var sel, range, textNode;
  if (window.getSelection) {
    sel = window.getSelection();
    if (sel.getRangeAt && sel.rangeCount) {
      range = sel.getRangeAt(0).cloneRange();
      range.deleteContents();
      textNode = document.createTextNode(text);
      range.insertNode(textNode);

      // Move caret to the end of the newly inserted text node
      range.setStart(textNode, textNode.length);
      range.setEnd(textNode, textNode.length);
      sel.removeAllRanges();
      sel.addRange(range);
    }
  } else if (document.selection && document.selection.createRange) {
    range = document.selection.createRange();
    range.pasteHTML(text);
  }
}


$("#editable").keypress(function(evt) {
  if (evt.which) {
    var charStr = String.fromCharCode(evt.which);
    var transformedChar = transformTypedCharacter(charStr);
    if (transformedChar != charStr) {
      insertTextAtCursor(transformedChar);
      return false;
    } 
  }
});

Source: (StackOverflow)

Stop div from showing more than once per session or visit

I have a JS function that shows a message div on doc ready and then hides it if the 'close' link is clicked.

I would like to show this message div only once per visit to the first page the user visits and then then never for any page view in the site after that.

At the moment when a user clicks on another page within the site, the message div shows again and this is annoying obviously for everyone.

I looked up 'one' in jQuery but am not sure how to implement it with my low JS knowledge.

<script>
$(document).ready(function(){
    $("div#panel").hide();

    var autoTimer = null;

    autoTimer = setTimeout(function(){
        $("div#panel").slideDown("slow");
    },1000);
    $("#close").click(function(){
        $("div#panel").slideUp("slow"); 
        if(autoTimer) clearTimeout(autoTimer);
        autoTimer = null;
        $("div#close").css("display","none");
    });         
});
</script>

Source: (StackOverflow)

what is it used for _.once in underscore?

I just look at the once API of source in underscore.js, then wandering what is it the used for in the method, it seems doing nothing:

func = null

the source:

  _.once = function(func) {
    var ran = false, memo;
    return function() {
      if (ran) return memo;
      ran = true;
      memo = func.apply(this, arguments);
      func = null;
      return memo;
    };
  };

Source: (StackOverflow)

Why doesn't my Node.js process terminate once all listeners have been removed?

In the following code, I assign a listener to the data event of process.stdin with the once method.

console.log('Press Enter to allow process to terminate')
process.stdin.once('data', callback)

function callback (data) {
    console.log('Process can terminate now')
}

In theory, when the callback has fired, the event listener should be automatically removed (because I attached it with once), allowing the process to terminate. Surprisingly, in this case, the process never terminates (The code you see is the whole thing, try it!). I also tried manually removing the listener, but that changes nothing.

Is there something else going on here that I don't realise perhaps?


Source: (StackOverflow)

Distinguish one sub invocation from another

In the following fragment, how can I distinguish the second invocation instance of my sub foo from the first?

while ($whatever) {
  foo(); foo();     # foo() and foo() have the same caller package, file, and line
}

Something like a super-caller() that returned file, line and column would do the trick. I'd prefer not to use source filters.

Background, or, isn't this a bit of an XY Problem?

I have a convenience module, Local::Thread::Once, that exposes functionality like pthread_once/std::call_once in an OO-ish way and also as a subroutine attribute. These are easy enough, since there is a natural and unambiguous "once_control" or "once_flag" in either case.

However, there is additionally a procedural interface — once { ... } — that currently serializes based on the $filename and $line returned by caller. Something like this:

sub once(&) {
  my $user_routine = shift;
  my (undef, $file, $line) = caller;

  my $once_control = get_a_shared_flag_just_for_this_invocation($file, $line);

  lock($once_control);
  if (! $once_control) { $once_control++; $user_routine->(); }
  return;
}

That's not precisely how it works — the real one is more efficient — but the point, again, is that invocation is keyed off of the file and line of the caller. This works, except that it cannot distinguish two invocations on the same line.

while ($whatever) {
  once { foo(); }
  once { bar(); }                    # OK, foo() and bar() each called only once
  once { baz(); }; once { buz(); };  # :(  buz() not called, not even once
}

Note that the address of $user_routine cannot be used as an additional discriminant, since subs are copied from one ithread to another.

I can live with this problem as a documented limitation for a very contrived use case, but I'd prefer to fix it somehow.


Source: (StackOverflow)

Showing the setup screen only on first launch in android

I am making an android application but i can't figure out how i can make the setup screen show up only the first time. This is how the application is going to work: User launches the application after installation and is being shown the welcome/setup screen. And once the user is done with the setup, the setup screens will never appear again unless the user reinstalls the application.

How can i make this happen??? Please help and thanks SO much in advance!


Source: (StackOverflow)

jQuery - Set timer to run once then execute a function

So I have another plugin that takes a second to load because it has to get some info, so I plan on making it so that it runs on about a 3 second timer, during the 3 seconds it will display something like "Loading Statistics" then slides down the statistics judging that they took 3 seconds or less to load, heres my current code, but it keeps repeating http://jsfiddle.net/7tkGY/

Thanks =) - Necro


Source: (StackOverflow)

Show welcome div only once per user / browser session

I want to show a welcome div only once per user or session. I know that there is Jquery option. Since am a newbie to jquery, I have not been able to resolve it myself. Please help

Here is the html and script to hide the div

 <div class="welcome">
    <div>
    <h1>Hello..!<br> Welcome to ABC
    <br>
    </h1>
    <h2>We wish you a Great Day..!</h2>
    <br>

    <h2><a id="close-welcome" rel='nofollow' href="#">Thank you.. and please take me to the     website</a> </h2>
    </div>
   </div>

   <script>
$(document).ready (function() {
$("#close-welcome").click (function() {
$(".welcome").fadeOut (1000);


});

});
</script>

I want to show this welcome page only once, till the user closes the browser.. Awaiting valuable help


Source: (StackOverflow)

Detach and re-attach mousewheel event listener don't reset scroll inertia

I'm using the jquery-mousewheel plugin to trigger a function.

When I call moveit I detach the listener and wait until the animation is completed, then I re-attach the listener.

The problem is that when I re-attach it, the mousewheel plugin is still listening to the inertia of some mouses/trackpads, and call moveit repeatedly.

I guess debouncing or throttling the function call are not good solutions in my specific case, because the inertia is still there, and I also want the listener to be attached immediately for other possible moveit calls.

Is there a way to "kill the inertia" by completely resetting the mousewheel event, instead of only detaching it?

$(document).ready(function () {

    var tween;
    var slide = $('#slide');

    function bodyListen () {
        $('body').on('mousewheel.bodyscroll',
        function (e, delta, deltaX, deltaY) {
            e.preventDefault();
            $('body').off('mousewheel.bodyscroll');
            moveit();
        });
    }

    function moveit () {
        tween = TweenMax.to(slide, 0.8, {
            marginLeft: 300,
            onComplete: bodyListen
        });
    }

    bodyListen();
});

Source: (StackOverflow)

execute function just once

I read this thread and tried to use Ted Hopps method function in javascript that can be called only once

But seems that i can't make it work properly. I want to create a div with JS by onclick="function()" on an image. But i want to do that just once.

 function runOnce(f){           

    var executed = false;
    return function () {
        if (!executed) {
            executed = true;
         return f.apply(this,arguments);
        }
    };
}
function rollover() { 
var new1=runOnce(myfunction);
new1(1);
} 

function myfunction(){


var tr1 = document.createElement("tr1");
tr1.style.width = "50%";
tr1.style.height = "100%";
tr1.style.background = "red";
tr1.style.color = "white";
tr1.style.right= "0px"
tr1.style.top= "0px"
tr1.style.padding= "2%"
tr1.style.opacity= "0.6"
tr1.style.position = "fixed";
tr1.innerHTML = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";

document.body.appendChild(tr1);



}

I call the rollover function from onclick

<img class="trans01button" onclick="rollover(1)" src="images/backgroundfragments_03.jpg" alt="Translation games" >

My problem is that it still executes the function multiple times. I made a jfiddle http://jsfiddle.net/RE3fp/ here but i can't make it work while my dreamweaver version works. I mean i can't make the "myfunction" div even to appear, while my problem is that it executes everytime i click the image.


Source: (StackOverflow)

Using jQuery to detect a mouse scroll per instance, not per click

What I'm attempting to do is move an element while a user scrolls up or down. I have the mechanics of this within a function called goTo(). I am also trying to track where the user is by using a variable. When the user scrolls up or down, the variable goes up or down by one. This is what my function looks like so far:

$(window).on('DOMMouseScroll mousewheel', function(event){

 clearTimeout($.data(this, 'timer')); // NEW CODE 

 $.data(this, 'timer', setTimeout(function() { // NEW CODE

    if( event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0 )
    {
        --curScl;
        goTo(curScl);

    } else {
        ++curScl;
        goTo(curScl);
    }

 }, 250)); // NEW CODE 

});

UPDATE: Per the advice of @Bart Jedrocha, I've added a setTimeout wrapper that keeps the value of curSel from skyrocketing with every scroll. However, there is a delay between when the scroll is executed, and when the element moves. It feels very unnatural. Is there a way to do this without the delay?


Source: (StackOverflow)

Prolog - subtract each item only once

when I use prolog's built-in predicate "subtract/3" : subtract(+Set, +Delete, -Result) in for example:

subtract([a,b,c,d,c,c,d,e], [c,a], X).  
X = [b, d, d, e].  

but I want to subtract each item in +Delete from +Set ONCE. I mean, I want

subtract([a,b,c,d,c,c,d,e], [c,a], X). to give
X = [b, d, c, c, d, e].

How can I do this?


Source: (StackOverflow)