• Login
  • login to access premium content (it's free).

Code Snippets

This page contains a free collection of code snippets.

Please click here to submit code to this collection.


Test

Author: Terry Riegel

HTML/OS code

test

Notes

na

setTimeout() with scope.

Author: Terry Riegel

Javascript code

function blah(){
 var i=4; // I want to use i in my setTimeout
 setTimeout((function(){return function(){alert(i);}  })() ,500);
}

Notes

This is a bugger. I know what this does but the syntax eludes me every time...

Have a variable that you want be available to a setTimeout. This will do it.

What it does is creates an anonymous function that executes immediatly. Its purpose is to trigger a closure. setTimeout expects a function passed as its first paramater so our executing function will return the function that will run at the end of the timeout.

This stuff is cool but takes time to think through.

TEST:Page Resilient Messages

Author: Terry Riegel

HTML/OS code

#  Usage:
#  <html>
#   <<message(0)>>
# </html>
# <<overlay dosomething
#  message('Operation Successful')
#  goto page
# >>


function message(themsg) locals x do
 x='MSG'+replaceall(page,'/','.')
 if themsg='' or themsg='ERROR' or themsg=0 then
  themsg=@x
  @x='ERROR'
 else
  @x=themsg
 /if
 if themsg='ERROR' then themsg='' /if
 return themsg /return
/function

Notes

Anyone that has programmed with HTML/OS has undoubtedly encountered this pattern...

<<if message<>'ERROR' then display message /display /if message='ERROR'>>

It works to show the user the result of the operation they attempted to perform in an overlay. If the user opens another page that displays message, then it is consumed on the wrong page.

This code snip provides a better solution to this common HTML/OS pattern.


Submit Your Code

If you have a killer snip of HTML/OS code submit it here.

Submit A Snippet