Creating a new function and passing a variable


(Steve Warby) #1

I need to create the following:

function changeText(newText)

{
textPrice.innerHTML = newText;

}

Then call this function and pass a new value.

When I create a new function I cannot change the parameters in the function.

Where in the docs is all the relevant info please ?

Cheers

Steve Warby


#2

I need to know the same thing! Any information would be handy


(𝕄𝕚𝕔𝕙𝕒𝕖𝕝 𝔾𝕒𝕣𝕠𝕗𝕒𝕝𝕠) #3

I’m confused, you seem to be really close with your code. What is it that you can’t do?


(strmiska) #4

me, too. :stuck_out_tongue_winking_eye:
cretae a new function and call it "changeText"
create there your code
within hype you can call any function:
hypeDocument.functions().name(hypeDocument, element, event);//name = name of function


#5

Basically when you choose to trigger a function via the timeline actions there’s no option to pass a variable to the function. How can we do that?


(strmiska) #6

i think, if you might change the text by using a new var, you have to write a whole new function.
then you can trigger this to the timeline by using actions and doggle between this two keyframes on the timeline?
timeline: ----x(f1)-----x(f2)------ (paused on the specific keyfame)
button - goTo keyframe (time)


(Steve Warby) #7

Hi,

when I create a function I cannot change the parameters. e.g.

function showHomePage(hypeDocument,element,event)
{
hypeDocument.showSceneNamed(‘homePage’, hypeDocument.kSceneTransitionCrossfade, 1.1)
}

the parameter is read only i.e. ‘(hypeDocument,element,event)’

I therefore cannot change this to :slight_smile:
function changeText(newText)

{
textPrice.innerHTML = newText;

}

Unless I am doing something dumb here.

Cheers

Steve Warby


(strmiska) #8

not necessery!
create a function and write into:
`textPrice.innerHTML = newText;

go to hypes functionFolder and rename this function with: changeText()
then call this changeText from everywhere

or write complete function into changeText():
`window.function changeText(newText)

{
textPrice.innerHTML = newText;

}`


(Steve Warby) #9

Sorry I must be being really dumb here.

Can someone do a small app to show this.

How do I pass a variable to my function when I can change the parameters ?

Cheers

Steve Warb


#10

@strmiska, @Photics, (@agent.pires)

Guys! He is referring to the parameters / arguments in a function.

Basically:

function untitled(hypeDocument, element, event)

Here hypeDocument, element and event are the arguments that are passed to the function.
This is done via the Hype API. If they have not been declared then they are set to undefined. The API does not allow you to call a function with more than the specified amount of arguments. In that, I mean that you cannot pass an argument (parameter) within the hype’s functions() call. You can only pass the three that are available.

So @classic12 in answer to your question you cannot achieve what it is you are trying to achieve by passing an extra parameter / argument to a Hype function.

There are probably other ways to achieve what you want. If you can elaborate on what you are trying to do or share a document then I’m sure someone can come up with something.

You could do this:

NOTE All functions have a built in object (arguments). This object holds the values of arguments that were used when the function was called (invoked). You can use this to access the argument (provided you know what key or position the argument is in - easy if there is only one being used ;)) Example:

function changeText(hypeDocument, element, event) {
    
    textPrice.innerHTML = arguments[0];
}

Then when you call this function anywhere else in your document (in another function) like this:

hypeDocument.functions().changeText("$1.40");

then the value of the “textPrice” variable (element) will change to whatever is in the arguments. In this case $1.40.

arguments.hype.zip (15.2 KB)


Calling and Handling External Javascript with Parameters
ShowSceneNamed for generated button
(drewbullen) #11

You could also use a function expression and avoid having to specify hypeDocument.functions()…

Specify a function expression like this:

changeText = function (parameter) {
doSomething;
return something;
};

And call it from anywhere in your Hype animation like this:

changeText();

See here for more details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function


(Steve Warby) #12

I have the answer here.


(drewbullen) #13

This is exactly the same as the solution in the previous post - you had the answer weeks ago.